Fixed loading multiple songs issue.
This commit is contained in:
@@ -6,6 +6,7 @@ namespace Harmonia.Core.Engine;
|
|||||||
public class BassAudioEngine : IAudioEngine, IDisposable
|
public class BassAudioEngine : IAudioEngine, IDisposable
|
||||||
{
|
{
|
||||||
private readonly BaseMediaPlayer _mediaPlayer;
|
private readonly BaseMediaPlayer _mediaPlayer;
|
||||||
|
private readonly SemaphoreSlim _loadLock = new(1, 1);
|
||||||
|
|
||||||
private CancellationTokenSource? _cancellationTokenSource;
|
private CancellationTokenSource? _cancellationTokenSource;
|
||||||
|
|
||||||
@@ -136,9 +137,20 @@ public class BassAudioEngine : IAudioEngine, IDisposable
|
|||||||
private async Task<bool> LoadWaveSourceAsync(string fileName)
|
private async Task<bool> LoadWaveSourceAsync(string fileName)
|
||||||
{
|
{
|
||||||
_cancellationTokenSource?.Cancel();
|
_cancellationTokenSource?.Cancel();
|
||||||
_cancellationTokenSource = new CancellationTokenSource();
|
|
||||||
|
|
||||||
CancellationToken token = _cancellationTokenSource.Token;
|
CancellationTokenSource cancellationTokenSource = new();
|
||||||
|
_cancellationTokenSource = cancellationTokenSource;
|
||||||
|
|
||||||
|
CancellationToken token = cancellationTokenSource.Token;
|
||||||
|
|
||||||
|
// Serialize loads so overlapping MediaPlayer.LoadAsync calls can't
|
||||||
|
// race and leave orphaned streams playing.
|
||||||
|
await _loadLock.WaitAsync(CancellationToken.None);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (token.IsCancellationRequested)
|
||||||
|
return false;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -154,10 +166,20 @@ public class BassAudioEngine : IAudioEngine, IDisposable
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (token.IsCancellationRequested)
|
if (token.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
// A newer load superseded this one; make sure this stream
|
||||||
|
// doesn't keep playing in the background.
|
||||||
|
_mediaPlayer.Stop();
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_loadLock.Release();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void UpdateSource(string fileName)
|
private void UpdateSource(string fileName)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ public class AudioPlayer : IAudioPlayer
|
|||||||
private readonly IAudioEngine _audioEngine;
|
private readonly IAudioEngine _audioEngine;
|
||||||
private readonly IPlaylistManager _playlistManager;
|
private readonly IPlaylistManager _playlistManager;
|
||||||
|
|
||||||
|
private int _loadVersion;
|
||||||
|
|
||||||
private Playlist? _playlist;
|
private Playlist? _playlist;
|
||||||
public Playlist? Playlist
|
public Playlist? Playlist
|
||||||
{
|
{
|
||||||
@@ -241,8 +243,15 @@ public class AudioPlayer : IAudioPlayer
|
|||||||
|
|
||||||
CurrentPlaylistSong = song;
|
CurrentPlaylistSong = song;
|
||||||
|
|
||||||
|
int loadVersion = Interlocked.Increment(ref _loadVersion);
|
||||||
|
|
||||||
bool isLoaded = await TryLoadAsync(song);
|
bool isLoaded = await TryLoadAsync(song);
|
||||||
|
|
||||||
|
// A newer load request was started while this one was in flight;
|
||||||
|
// abandon this one so only the latest request controls playback.
|
||||||
|
if (loadVersion != Volatile.Read(ref _loadVersion))
|
||||||
|
return false;
|
||||||
|
|
||||||
if (isLoaded == false)
|
if (isLoaded == false)
|
||||||
{
|
{
|
||||||
if (mode == PlaybackMode.LoadAndPlay)
|
if (mode == PlaybackMode.LoadAndPlay)
|
||||||
|
|||||||
Reference in New Issue
Block a user