fix: enable player controls on load, clear track selection on stop and end-of-track
Add StateChanged multicast event to IPlayerService so AudioPlayerBar and TracksView re-render themselves without relying on the IsFixed cascade re-render path. Clear _selectedTrack in TracksView when IsLoaded drops (stop, unload, end-of-track). Set IsLoaded=false in OnPlaybackEndCallback so end-of-track triggers the same clear path. Add JS-module readiness probe in AudioInteropService; delete dead TS and buffered C# path; consolidate GetPlayIcon/FormatTime helpers; fix misleading minimize dock icon.
This commit is contained in:
@@ -16,6 +16,15 @@ public class AudioInteropService : IAsyncDisposable
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!await WaitForModuleReadyAsync())
|
||||
{
|
||||
return new AudioOperationResult
|
||||
{
|
||||
Success = false,
|
||||
Error = "Audio engine failed to load (timed out waiting for DeepDrftAudio module)."
|
||||
};
|
||||
}
|
||||
|
||||
var result = await _jsRuntime.InvokeAsync<AudioOperationResult>("DeepDrftAudio.createPlayer", playerId);
|
||||
return result;
|
||||
}
|
||||
@@ -25,19 +34,34 @@ public class AudioInteropService : IAsyncDisposable
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<AudioOperationResult> InitializeBufferedPlayerAsync(string playerId)
|
||||
// The audio engine is loaded as an ES module via a deferred `import(...)` in
|
||||
// App.razor, so on a slow WASM boot or cache miss it may not have executed by
|
||||
// the time the first track is selected. Poll its readiness probe (tolerating
|
||||
// the window before `window.DeepDrftAudio` even exists, when the call throws)
|
||||
// up to a short timeout before the first interop call.
|
||||
private async Task<bool> WaitForModuleReadyAsync()
|
||||
{
|
||||
return await InvokeJsAsync<AudioOperationResult>("DeepDrftAudio.initializeBufferedPlayer", playerId);
|
||||
}
|
||||
const int timeoutMs = 5000;
|
||||
const int pollIntervalMs = 50;
|
||||
var elapsed = 0;
|
||||
|
||||
public async Task<AudioOperationResult> AppendAudioBlockAsync(string playerId, byte[] audioBlock)
|
||||
{
|
||||
return await InvokeJsAsync<AudioOperationResult>("DeepDrftAudio.appendAudioBlock", playerId, audioBlock);
|
||||
}
|
||||
while (elapsed < timeoutMs)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (await _jsRuntime.InvokeAsync<bool>("DeepDrftAudio.isReady"))
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// window.DeepDrftAudio not attached yet — keep polling until timeout.
|
||||
}
|
||||
|
||||
public async Task<AudioLoadResult> FinalizeAudioBufferAsync(string playerId)
|
||||
{
|
||||
return await InvokeJsAsync<AudioLoadResult>("DeepDrftAudio.finalizeAudioBuffer", playerId);
|
||||
await Task.Delay(pollIntervalMs);
|
||||
elapsed += pollIntervalMs;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Streaming methods
|
||||
@@ -238,8 +262,6 @@ public class AudioInteropService : IAsyncDisposable
|
||||
{
|
||||
if (typeof(T) == typeof(AudioOperationResult))
|
||||
return (T)(object)new AudioOperationResult { Success = false, Error = ex.Message };
|
||||
if (typeof(T) == typeof(AudioLoadResult))
|
||||
return (T)(object)new AudioLoadResult { Success = false, Error = ex.Message };
|
||||
if (typeof(T) == typeof(StreamingResult))
|
||||
return (T)(object)new StreamingResult { Success = false, Error = ex.Message };
|
||||
if (typeof(T) == typeof(SeekResult))
|
||||
@@ -331,14 +353,6 @@ public class SeekResult : AudioOperationResult
|
||||
public long ByteOffset { get; set; }
|
||||
}
|
||||
|
||||
public class AudioLoadResult : AudioOperationResult
|
||||
{
|
||||
public double Duration { get; set; }
|
||||
public int SampleRate { get; set; }
|
||||
public int NumberOfChannels { get; set; }
|
||||
public double LoadProgress { get; set; }
|
||||
}
|
||||
|
||||
public class StreamingResult : AudioOperationResult
|
||||
{
|
||||
public bool CanStartStreaming { get; set; }
|
||||
|
||||
@@ -2,7 +2,6 @@ using DeepDrftModels.DTOs;
|
||||
using DeepDrftPublic.Client.Clients;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using NetBlocks.Models;
|
||||
using System.Buffers;
|
||||
|
||||
namespace DeepDrftPublic.Client.Services;
|
||||
|
||||
@@ -38,6 +37,9 @@ public abstract class AudioPlayerService : IPlayerService, IAsyncDisposable
|
||||
public EventCallback? OnStateChanged { get; set; }
|
||||
public EventCallback? OnTrackSelected { get; set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public event Action? StateChanged;
|
||||
|
||||
protected AudioPlayerService(AudioInteropService audioInterop, TrackMediaClient trackMediaClient)
|
||||
{
|
||||
_audioInterop = audioInterop;
|
||||
@@ -74,134 +76,16 @@ public abstract class AudioPlayerService : IPlayerService, IAsyncDisposable
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task SelectTrack(TrackDto track)
|
||||
{
|
||||
await EnsureInitializedAsync();
|
||||
|
||||
await NotifyStateChanged();
|
||||
|
||||
if (OnTrackSelected.HasValue)
|
||||
await OnTrackSelected.Value.InvokeAsync();
|
||||
|
||||
await LoadTrack(track);
|
||||
await NotifyStateChanged();
|
||||
}
|
||||
|
||||
private async Task LoadTrack(TrackDto track)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (IsLoading) return;
|
||||
|
||||
if (IsPlaying || IsPaused)
|
||||
{
|
||||
await Unload();
|
||||
}
|
||||
|
||||
// Reset state to indicate loading has started
|
||||
ErrorMessage = null;
|
||||
LoadProgress = 0;
|
||||
IsLoaded = false;
|
||||
IsLoading = true;
|
||||
Duration = null;
|
||||
CurrentTime = 0;
|
||||
await NotifyStateChanged();
|
||||
|
||||
var loadResult = await _audioInterop.InitializeBufferedPlayerAsync(PlayerId);
|
||||
if (loadResult?.Success != true)
|
||||
{
|
||||
ErrorMessage = $"Failed to initialize audio buffer: {loadResult?.Error ?? "Unknown error"}";
|
||||
return;
|
||||
}
|
||||
|
||||
var mediaResult = await _trackMediaClient.GetTrackMedia(track.EntryKey);
|
||||
if (!mediaResult.Success)
|
||||
{
|
||||
ErrorMessage = mediaResult.GetMessage();
|
||||
return;
|
||||
}
|
||||
|
||||
if (mediaResult.Value == null)
|
||||
{
|
||||
ErrorMessage = "No audio returned from server";
|
||||
return;
|
||||
}
|
||||
|
||||
TrackMediaResponse audio = mediaResult.Value;
|
||||
await StreamAudio(audio);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ErrorMessage = $"Error loading audio: {ex.Message}";
|
||||
LoadProgress = 0;
|
||||
IsLoaded = false;
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsLoading = false;
|
||||
await NotifyStateChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task StreamAudio(TrackMediaResponse audio)
|
||||
{
|
||||
const int bufferSize = 32 * 1024;
|
||||
var rentedBuffer = ArrayPool<byte>.Shared.Rent(bufferSize);
|
||||
try
|
||||
{
|
||||
long totalBytesRead = 0;
|
||||
int currentBytes;
|
||||
|
||||
do
|
||||
{
|
||||
currentBytes = await audio.Stream.ReadAsync(rentedBuffer, 0, bufferSize);
|
||||
|
||||
if (currentBytes > 0)
|
||||
{
|
||||
totalBytesRead += currentBytes;
|
||||
|
||||
// Slice to actual bytes read before sending to interop
|
||||
var chunk = rentedBuffer[..currentBytes];
|
||||
|
||||
var appendResult = await _audioInterop.AppendAudioBlockAsync(PlayerId, chunk);
|
||||
if (!appendResult.Success)
|
||||
{
|
||||
throw new Exception($"Failed to append audio block: {appendResult.Error}");
|
||||
}
|
||||
|
||||
if (audio.ContentLength > 0)
|
||||
{
|
||||
LoadProgress = Math.Min(1.0, (double)totalBytesRead / audio.ContentLength);
|
||||
await NotifyStateChanged();
|
||||
}
|
||||
}
|
||||
} while (currentBytes > 0);
|
||||
|
||||
var finalizeResult = await _audioInterop.FinalizeAudioBufferAsync(PlayerId);
|
||||
if (!finalizeResult.Success)
|
||||
{
|
||||
throw new Exception($"Failed to finalize audio buffer: {finalizeResult.Error}");
|
||||
}
|
||||
|
||||
Duration = finalizeResult.Duration;
|
||||
LoadProgress = 1.0;
|
||||
IsLoaded = true;
|
||||
ErrorMessage = null;
|
||||
await NotifyStateChanged();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ErrorMessage = $"Error streaming audio: {ex.Message}";
|
||||
LoadProgress = 0;
|
||||
IsLoaded = false;
|
||||
await NotifyStateChanged();
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
ArrayPool<byte>.Shared.Return(rentedBuffer);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Selecting a track is only supported through the streaming path. The former
|
||||
/// base buffered implementation drove JS no-ops (<c>initializeBufferedPlayer</c> /
|
||||
/// <c>appendAudioBlock</c> / <c>finalizeAudioBuffer</c>) and silently played
|
||||
/// silence. Subclasses must override with a real load path (see
|
||||
/// <see cref="StreamingAudioPlayerService.SelectTrack"/>).
|
||||
/// </summary>
|
||||
public virtual Task SelectTrack(TrackDto track) =>
|
||||
throw new NotSupportedException(
|
||||
"The base buffered player path is not implemented. Use a streaming player (SelectTrackStreaming).");
|
||||
|
||||
public async Task TogglePlayPause()
|
||||
{
|
||||
@@ -377,7 +261,9 @@ public abstract class AudioPlayerService : IPlayerService, IAsyncDisposable
|
||||
{
|
||||
IsPlaying = false;
|
||||
IsPaused = false;
|
||||
IsLoaded = false;
|
||||
CurrentTime = 0;
|
||||
Duration = null;
|
||||
await NotifyStateChanged();
|
||||
}
|
||||
|
||||
@@ -394,6 +280,7 @@ public abstract class AudioPlayerService : IPlayerService, IAsyncDisposable
|
||||
{
|
||||
if (OnStateChanged.HasValue)
|
||||
await OnStateChanged.Value.InvokeAsync();
|
||||
StateChanged?.Invoke();
|
||||
}
|
||||
|
||||
protected async Task NotifyTrackSelected()
|
||||
|
||||
@@ -22,6 +22,16 @@ public interface IPlayerService
|
||||
// Events for UI updates
|
||||
EventCallback? OnStateChanged { get; set; }
|
||||
EventCallback? OnTrackSelected { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Multicast side-channel for state changes. The provider owns the single
|
||||
/// <see cref="OnStateChanged"/> EventCallback (it drives the provider re-render);
|
||||
/// cascade consumers that read state directly off this service — and so are not
|
||||
/// re-rendered by the provider's render when the cascade is <c>IsFixed</c> —
|
||||
/// subscribe here to re-render themselves. Fires on the same cadence as
|
||||
/// <see cref="OnStateChanged"/> (throttled to ~10/s during streaming).
|
||||
/// </summary>
|
||||
event Action? StateChanged;
|
||||
|
||||
// Control methods
|
||||
Task InitializeAsync();
|
||||
|
||||
Reference in New Issue
Block a user