Сохранение громкости плеера
This commit is contained in:
@@ -59,6 +59,7 @@
|
||||
|
||||
[Inject] protected IJSRuntime JS { get; set; } = null!;
|
||||
[Inject] private TokenStorage TokenStorage { get; set; } = null!;
|
||||
[Inject] private PlayerStorage PlayerStorage { get; set; } = null!;
|
||||
[Inject] private AuthenticationStateProvider AuthProvider { get; set; } = null!;
|
||||
[Inject] private ISnackbar Snackbar { get; set; } = null!;
|
||||
|
||||
@@ -76,6 +77,8 @@
|
||||
if (firstRender)
|
||||
{
|
||||
await EnsureAudioModuleAsync();
|
||||
await ChangeVolume(await PlayerStorage.GetVolumeAsync());
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,6 +229,8 @@
|
||||
var volume = value / 100;
|
||||
await _audioElement.InvokeVoidAsync("setVolume", volume);
|
||||
_isMuted = false;
|
||||
_currentVolume = value;
|
||||
await PlayerStorage.SetVolumeAsync(value);
|
||||
StateHasChanged();
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
@@ -21,6 +21,7 @@ internal class Program
|
||||
});
|
||||
|
||||
builder.Services.AddScoped<TokenStorage>();
|
||||
builder.Services.AddScoped<PlayerStorage>();
|
||||
builder.Services.AddScoped<AuthStateProvider>();
|
||||
builder.Services.AddScoped<AuthenticationStateProvider>(sp => sp.GetRequiredService<AuthStateProvider>());
|
||||
builder.Services.AddScoped<ApiClient>();
|
||||
|
||||
30
PlaylistShared.Pwa/Services/PlayerStorage.cs
Normal file
30
PlaylistShared.Pwa/Services/PlayerStorage.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using Microsoft.JSInterop;
|
||||
|
||||
namespace PlaylistShared.Pwa.Services;
|
||||
|
||||
public class PlayerStorage
|
||||
{
|
||||
private readonly IJSRuntime _js;
|
||||
private const string VolumeKey = "audio_player_volume";
|
||||
|
||||
public PlayerStorage(IJSRuntime js) => _js = js;
|
||||
|
||||
public async Task SetVolumeAsync(double volume)
|
||||
{
|
||||
await _js.InvokeVoidAsync("localStorage.setItem", VolumeKey, volume);
|
||||
}
|
||||
|
||||
public async Task<double> GetVolumeAsync()
|
||||
{
|
||||
var volume = await _js.InvokeAsync<string>("localStorage.getItem", VolumeKey);
|
||||
|
||||
if (double.TryParse(volume, out var result))
|
||||
{
|
||||
result = Math.Clamp(result, 0, 100);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user