diff --git a/DeepDrftWeb.Client/Controls/AudioPlayerBar/AudioPlayerBar.razor b/DeepDrftWeb.Client/Controls/AudioPlayerBar/AudioPlayerBar.razor index e52e1f3..2765ee0 100644 --- a/DeepDrftWeb.Client/Controls/AudioPlayerBar/AudioPlayerBar.razor +++ b/DeepDrftWeb.Client/Controls/AudioPlayerBar/AudioPlayerBar.razor @@ -23,7 +23,14 @@ else IsLoaded="IsLoaded" TogglePlayPause="@TogglePlayPause" Stop="@Stop"/> - + @if (IsLoading) + { + + } diff --git a/DeepDrftWeb.Client/Controls/TrackCard.razor b/DeepDrftWeb.Client/Controls/TrackCard.razor index 2b675d1..03879a4 100644 --- a/DeepDrftWeb.Client/Controls/TrackCard.razor +++ b/DeepDrftWeb.Client/Controls/TrackCard.razor @@ -16,13 +16,13 @@
- @TrackModel?.TrackName - @TrackModel?.Artist diff --git a/DeepDrftWeb.Client/Services/AudioPlaybackEngine.cs b/DeepDrftWeb.Client/Services/AudioPlaybackEngine.cs deleted file mode 100644 index ee420be..0000000 --- a/DeepDrftWeb.Client/Services/AudioPlaybackEngine.cs +++ /dev/null @@ -1,364 +0,0 @@ -// DEPRECATED: This class has been merged into AudioPlayerService -// TODO: Remove after testing new implementation -/* -using DeepDrftModels.Entities; -using DeepDrftWeb.Client.Clients; -using NetBlocks.Models; - -namespace DeepDrftWeb.Client.Services; - -public class AudioPlaybackEngine : IAsyncDisposable -{ - public event Events.EventAsync? OnProgressChanged; - public event Events.EventAsync? OnLoadChanged; - public event Events.EventAsync? OnPlaybackEnded; - - public required TrackMediaClient Client { get; set; } - public required AudioInteropService AudioInterop { get; set; } - - public string PlayerId { get; private set; } = Guid.NewGuid().ToString(); - public bool IsInitialized { get; private set; } = false; - public bool IsLoaded { get; private set; } = false; - public bool IsLoading { get; private set; } = false; - public bool IsPlaying { get; private set; } = false; - public bool IsPaused { get; private set; } = false; - public double CurrentTime { get; private set; } = 0; - public double? Duration { get; private set; } = null; - public double Volume { get; private set; } = 0.8; - public double LoadProgress { get; private set; } = 0; - public string? ErrorMessage { get; private set; } - - public AudioPlaybackEngine(AudioInteropService audioInterop, TrackMediaClient client) - { - AudioInterop = audioInterop; - Client = client; - } - - public async Task InitializeAudioPlayer() - { - if (IsInitialized) return; - - var result = await AudioInterop.CreatePlayerAsync(PlayerId); - if (!result.Success) - { - ErrorMessage = $"Failed to initialize audio player: {result.Error}"; - return; - } - - await AudioInterop.SetOnProgressCallbackAsync(PlayerId, OnProgress); - await AudioInterop.SetOnEndCallbackAsync(PlayerId, OnPlaybackEnd); - await AudioInterop.SetOnLoadProgressCallbackAsync(PlayerId, OnLoadProgress); - - await AudioInterop.SetVolumeAsync(PlayerId, Volume); - - IsInitialized = true; - } - - public async Task LoadTrack(TrackEntity track) - { - TrackMediaResponse? audio = null; - try - { - // Immediately reset state to indicate loading has started - ErrorMessage = null; - LoadProgress = 0; - IsLoaded = false; - IsLoading = true; - Duration = null; - CurrentTime = 0; - - // Trigger load event immediately to show loading state in UI - if (OnLoadChanged != null) await OnLoadChanged.Invoke(0); - - if (IsPlaying || IsPaused) - { - // If we were playing/paused, unload the current track - await Unload(); - } - - AudioOperationResult? loadResult = await AudioInterop.InitializeBufferedPlayerAsync(PlayerId); - if (loadResult?.Success != true) - { - ErrorMessage = $"Failed to initialize audio buffer: {loadResult?.Error ?? "Unknown error"}"; - return; - } - - var mediaResult = await Client.GetTrackMedia(track.EntryKey); - if (!mediaResult.Success) - { - ErrorMessage = mediaResult.GetMessage(); - return; - } - - if (mediaResult.Value == null) - { - ErrorMessage = "No audio returned from server"; - return; - } - audio = mediaResult.Value; - } - catch (Exception ex) - { - ErrorMessage = $"Error loading audio: {ex.Message}"; - LoadProgress = 0; - IsLoaded = false; - } - finally - { - IsLoading = false; - } - - try - { - if (audio == null) return; - - await StreamAndPlay(audio); - } - catch (Exception ex) - { - ErrorMessage = $"Error streaming audio: {ex.Message}"; - } - } - - private async Task StreamAndPlay(TrackMediaResponse audio) - { - try - { - const int bufferSize = 32 * 1024; // Increased buffer size for better performance - long totalBytesRead = 0; - int currentBytes; - - do - { - var buffer = new byte[bufferSize]; - currentBytes = await audio.Stream.ReadAsync(buffer, 0, buffer.Length); - - if (currentBytes > 0) - { - totalBytesRead += currentBytes; - - // Resize buffer if we didn't read the full amount - if (currentBytes < bufferSize) - { - var trimmedBuffer = new byte[currentBytes]; - Array.Copy(buffer, trimmedBuffer, currentBytes); - buffer = trimmedBuffer; - } - - var appendResult = await AudioInterop.AppendAudioBlockAsync(PlayerId, buffer); - if (!appendResult.Success) - { - throw new Exception($"Failed to append audio block: {appendResult.Error}"); - } - - // Update progress during streaming - if (audio.ContentLength > 0) - { - LoadProgress = Math.Min(1.0, (double)totalBytesRead / audio.ContentLength); - if (OnLoadChanged != null) await OnLoadChanged.Invoke(LoadProgress); - } - } - } while (currentBytes > 0); - - // Finalize the buffer and update metadata - var finalizeResult = await AudioInterop.FinalizeAudioBufferAsync(PlayerId); - if (!finalizeResult.Success) - { - throw new Exception($"Failed to finalize audio buffer: {finalizeResult.Error}"); - } - - // Update engine state with audio metadata - Duration = finalizeResult.Duration; - LoadProgress = 1.0; - IsLoaded = true; - ErrorMessage = null; - - // Trigger final load completion event - if (OnLoadChanged != null) await OnLoadChanged.Invoke(1.0); - } - catch (Exception ex) - { - ErrorMessage = $"Error streaming audio: {ex.Message}"; - LoadProgress = 0; - IsLoaded = false; - throw; - } - } - - public async Task TogglePlayPause() - { - if (!IsLoaded) return; - - try - { - AudioOperationResult result; - - if (IsPlaying) - { - result = await AudioInterop.PauseAsync(PlayerId); - if (result.Success) - { - IsPlaying = false; - IsPaused = true; - } - } - else - { - result = await AudioInterop.PlayAsync(PlayerId); - if (result.Success) - { - IsPlaying = true; - IsPaused = false; - } - } - - if (!result.Success) - { - ErrorMessage = $"Playback error: {result.Error}"; - } - else - { - ErrorMessage = null; - } - } - catch (Exception ex) - { - ErrorMessage = $"Error controlling playback: {ex.Message}"; - } - } - - public async Task Stop() - { - if (!IsLoaded) return; - - try - { - var result = await AudioInterop.StopAsync(PlayerId); - if (result.Success) - { - IsPlaying = false; - IsPaused = false; - CurrentTime = 0; - ErrorMessage = null; - } - else - { - ErrorMessage = $"Stop error: {result.Error}"; - } - } - catch (Exception ex) - { - ErrorMessage = $"Error stopping playback: {ex.Message}"; - } - } - - public async Task Unload() - { - if (!IsLoaded) return; - - try - { - await Stop(); - var result = await AudioInterop.UnloadAsync(PlayerId); - if (result.Success) - { - IsPlaying = false; - IsPaused = false; - CurrentTime = 0; - Duration = null; - LoadProgress = 0; - IsLoaded = false; - ErrorMessage = null; - } - else - { - ErrorMessage = $"Unload error: {result.Error}"; - } - } - catch (Exception ex) - { - ErrorMessage = $"Error unloading track: {ex.Message}"; - } - } - - public async Task OnSeek(double position) - { - if (!IsLoaded) return; - - try - { - var result = await AudioInterop.SeekAsync(PlayerId, position); - if (result.Success) - { - CurrentTime = position; - ErrorMessage = null; - } - else - { - ErrorMessage = $"Seek error: {result.Error}"; - } - } - catch (Exception ex) - { - ErrorMessage = $"Error seeking: {ex.Message}"; - } - } - - public async Task OnVolumeChange(double volume) - { - Volume = volume; - - if (IsLoaded) - { - try - { - var result = await AudioInterop.SetVolumeAsync(PlayerId, volume); - if (!result.Success) - { - ErrorMessage = $"Volume error: {result.Error}"; - } - } - catch (Exception ex) - { - ErrorMessage = $"Error setting volume: {ex.Message}"; - } - } - } - - private async Task OnProgress(double currentTime) - { - CurrentTime = currentTime; - if (OnProgressChanged != null) - { - await OnProgressChanged(currentTime); - } - } - - private async Task OnPlaybackEnd() - { - IsPlaying = false; - IsPaused = false; - CurrentTime = 0; - - if (OnPlaybackEnded != null) - { - await OnPlaybackEnded(); - } - } - - private async Task OnLoadProgress(double progress) - { - LoadProgress = progress; - } - - public void ClearError() - { - ErrorMessage = null; - } - - public async ValueTask DisposeAsync() - { - await AudioInterop.DisposePlayerAsync(PlayerId); - } -} -*/ \ No newline at end of file diff --git a/DeepDrftWeb.Client/Services/PlayerService.cs b/DeepDrftWeb.Client/Services/PlayerService.cs deleted file mode 100644 index e045662..0000000 --- a/DeepDrftWeb.Client/Services/PlayerService.cs +++ /dev/null @@ -1,135 +0,0 @@ -// DEPRECATED: This class has been replaced by AudioPlayerService -// TODO: Remove after testing new implementation -/* -using DeepDrftModels.Entities; -using NetBlocks.Models; - -namespace DeepDrftWeb.Client.Services; - -public class PlayerService : IPlayerService -{ - private AudioPlaybackEngine? _audioEngine; - private bool _isInitialized = false; - - public PlayerService() - { - // Parameterless constructor - AudioPlaybackEngine will be set during initialization - } - - // IPlayerService state properties with defensive checks - public bool IsInitialized => _isInitialized; - public bool IsLoaded => _isInitialized && _audioEngine?.IsLoaded == true; - public bool IsLoading => _isInitialized && _audioEngine?.IsLoading == true; - public bool IsPlaying => _isInitialized && _audioEngine?.IsPlaying == true; - public bool IsPaused => _isInitialized && _audioEngine?.IsPaused == true; - public double CurrentTime => _isInitialized ? _audioEngine?.CurrentTime ?? 0.0 : 0.0; - public double? Duration => _isInitialized ? _audioEngine?.Duration : null; - public double Volume => _isInitialized ? _audioEngine?.Volume ?? 0.8 : 0.8; - public double LoadProgress => _isInitialized ? _audioEngine?.LoadProgress ?? 0.0 : 0.0; - public string? ErrorMessage => _isInitialized ? _audioEngine?.ErrorMessage : null; - - public event Action? OnStateChanged; - public event Events.EventAsync? OnTrackSelected; - - public async Task SelectTrack(TrackEntity track) - { - if (!_isInitialized) - { - await EnsureInitializedAsync(); - } - - // Immediately notify UI that track selection is happening - OnStateChanged?.Invoke(); - - if (OnTrackSelected != null) await OnTrackSelected.Invoke(); - - if (_isInitialized && _audioEngine != null) - { - await _audioEngine.LoadTrack(track); - // Force a state change to ensure UI reflects final loaded state - OnStateChanged?.Invoke(); - } - } - - public async Task Stop() - { - if (!_isInitialized || _audioEngine == null) return; - - await _audioEngine.Stop(); - OnStateChanged?.Invoke(); - } - - public async Task Unload() - { - if (!_isInitialized || _audioEngine == null) return; - - await _audioEngine.Unload(); - OnStateChanged?.Invoke(); - } - - public async Task TogglePlayPause() - { - if (!_isInitialized || _audioEngine == null) return; - - await _audioEngine.TogglePlayPause(); - OnStateChanged?.Invoke(); - } - - public async Task Seek(double position) - { - if (!_isInitialized || _audioEngine == null) return; - - await _audioEngine.OnSeek(position); - OnStateChanged?.Invoke(); - } - - public async Task SetVolume(double volume) - { - if (!_isInitialized || _audioEngine == null) return; - - await _audioEngine.OnVolumeChange(volume); - OnStateChanged?.Invoke(); - } - - public void ClearError() - { - if (!_isInitialized || _audioEngine == null) return; - - _audioEngine.ClearError(); - OnStateChanged?.Invoke(); - } - - public async Task InitializeAsync(AudioPlaybackEngine audioEngine) - { - if (_isInitialized) return; - - _audioEngine = audioEngine; - - try - { - await _audioEngine.InitializeAudioPlayer(); - - // Wire up engine events to trigger state change notifications - _audioEngine.OnProgressChanged += async _ => OnStateChanged?.Invoke(); - _audioEngine.OnPlaybackEnded += async () => OnStateChanged?.Invoke(); - _audioEngine.OnLoadChanged += async _ => OnStateChanged?.Invoke(); - - _isInitialized = true; - OnStateChanged?.Invoke(); - } - catch (Exception ex) - { - // Log error but don't throw - allow UI to continue functioning - Console.WriteLine($"Failed to initialize audio engine: {ex.Message}"); - } - } - - private async Task EnsureInitializedAsync() - { - if (!_isInitialized && _audioEngine != null) - { - await InitializeAsync(_audioEngine); - } - } -} -*/ \ No newline at end of file