Доработан плеер
This commit is contained in:
@@ -10,15 +10,18 @@ public class AudioPlayerService : IAudioPlayerService
|
||||
private readonly TokenStorage _tokenStorage;
|
||||
private readonly ISnackbar _snackbar;
|
||||
private readonly HttpClient _http;
|
||||
private readonly PlayerStorage _playerStorage;
|
||||
|
||||
private string? _currentTrackId;
|
||||
private string? _currentTrackTitle;
|
||||
private string? _currentTrackCoverUrl;
|
||||
private bool _isPlaying;
|
||||
private double _currentVolume = 70;
|
||||
private double _currentVolume = 50;
|
||||
private double _currentProgress;
|
||||
private string _currentTime = "0:00";
|
||||
private string _totalTime = "0:00";
|
||||
private double _currentTime = 0;
|
||||
private double _totalTime = 0;
|
||||
private string _currentTimeString = "0:00";
|
||||
private string _totalTimeString = "0:00";
|
||||
|
||||
public string? CurrentTrackId => _currentTrackId;
|
||||
public string? CurrentTrackTitle => _currentTrackTitle;
|
||||
@@ -34,21 +37,42 @@ public class AudioPlayerService : IAudioPlayerService
|
||||
}
|
||||
}
|
||||
public double CurrentProgress => _currentProgress;
|
||||
public string CurrentTime => _currentTime;
|
||||
public string TotalTime => _totalTime;
|
||||
public double CurrentTime => _currentTime;
|
||||
public double TotalTime => _totalTime;
|
||||
public string CurrentTimeString => _currentTimeString;
|
||||
public string TotalTimeString => _totalTimeString;
|
||||
|
||||
public event Action? OnStateChanged;
|
||||
|
||||
public AudioPlayerService(TokenStorage tokenStorage, ISnackbar snackbar, HttpClient httpClient)
|
||||
public AudioPlayerService(TokenStorage tokenStorage, ISnackbar snackbar, HttpClient httpClient, PlayerStorage playerStorage)
|
||||
{
|
||||
_tokenStorage = tokenStorage;
|
||||
_snackbar = snackbar;
|
||||
_http = httpClient;
|
||||
_playerStorage = playerStorage;
|
||||
|
||||
_ = LoadVolume();
|
||||
}
|
||||
|
||||
private async Task LoadVolume()
|
||||
{
|
||||
var savedVolume = await _playerStorage.GetVolumeAsync();
|
||||
|
||||
if (savedVolume != null)
|
||||
{
|
||||
_currentVolume = savedVolume.Value;
|
||||
}
|
||||
}
|
||||
|
||||
// Внешние команды (вызываются из компонентов)
|
||||
public async Task LoadAndPlayAsync(string trackId, string? accessToken = null, string? playlistShareToken = null, string? title = null, string? coverUrl = null)
|
||||
{
|
||||
if (_currentTrackId == trackId)
|
||||
{
|
||||
await PlayAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
// Если accessToken не передан, пытаемся получить его из хранилища
|
||||
if (string.IsNullOrWhiteSpace(accessToken))
|
||||
{
|
||||
@@ -101,19 +125,10 @@ public class AudioPlayerService : IAudioPlayerService
|
||||
OnPauseRequested?.Invoke();
|
||||
}
|
||||
|
||||
public async Task StopAsync()
|
||||
{
|
||||
_isPlaying = false;
|
||||
_currentTrackId = null;
|
||||
_currentProgress = 0;
|
||||
_currentTime = "0:00";
|
||||
OnStateChanged?.Invoke();
|
||||
OnStopRequested?.Invoke();
|
||||
}
|
||||
|
||||
public async Task SeekToAsync(double percent)
|
||||
{
|
||||
OnSeekRequested?.Invoke(percent);
|
||||
var newTime = (percent / 100) * _totalTime;
|
||||
OnSeekRequested?.Invoke(newTime);
|
||||
}
|
||||
|
||||
public async Task SetVolumeAsync(double volume)
|
||||
@@ -121,13 +136,13 @@ public class AudioPlayerService : IAudioPlayerService
|
||||
_currentVolume = volume;
|
||||
OnStateChanged?.Invoke();
|
||||
OnVolumeChangeRequested?.Invoke(volume);
|
||||
await _playerStorage.SetVolumeAsync(volume);
|
||||
}
|
||||
|
||||
// События для связи с реальным AudioPlayer компонентом
|
||||
public event Func<string, string?, string?, Task>? OnLoadAndPlayRequested;
|
||||
public event Func<Task>? OnPlayRequested;
|
||||
public event Func<Task>? OnPauseRequested;
|
||||
public event Func<Task>? OnStopRequested;
|
||||
public event Func<double, Task>? OnSeekRequested;
|
||||
public event Func<double, Task>? OnVolumeChangeRequested;
|
||||
|
||||
@@ -144,11 +159,14 @@ public class AudioPlayerService : IAudioPlayerService
|
||||
OnStateChanged?.Invoke();
|
||||
}
|
||||
|
||||
public void UpdateProgress(double progress, string currentTime, string totalTime)
|
||||
public void UpdateProgress(double currentTime, double totalTime)
|
||||
{
|
||||
var progress = (currentTime / totalTime) * 100;
|
||||
_currentProgress = progress;
|
||||
_currentTime = currentTime;
|
||||
_currentTimeString = FormatDuration(currentTime);
|
||||
_totalTime = totalTime;
|
||||
_totalTimeString = FormatDuration(totalTime);
|
||||
OnStateChanged?.Invoke();
|
||||
}
|
||||
|
||||
@@ -157,7 +175,10 @@ public class AudioPlayerService : IAudioPlayerService
|
||||
_isPlaying = false;
|
||||
_currentTrackId = null;
|
||||
_currentProgress = 0;
|
||||
_currentTime = "0:00";
|
||||
_currentTime = 0;
|
||||
_currentTimeString = "0:00";
|
||||
_totalTime = 0;
|
||||
_currentTimeString = "0:00";
|
||||
OnStateChanged?.Invoke();
|
||||
}
|
||||
|
||||
@@ -183,4 +204,11 @@ public class AudioPlayerService : IAudioPlayerService
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private string FormatDuration(double seconds)
|
||||
{
|
||||
var mins = (int)(seconds / 60);
|
||||
var secs = (int)(seconds % 60);
|
||||
return $"{mins}:{secs:D2}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user