Stability fixes with streaming
This commit is contained in:
@ -16,6 +16,7 @@ namespace Aurora.Services.Player
|
||||
private MediaPlayer _mediaPlayer;
|
||||
private LibVLC _libvlc;
|
||||
private PlaybackState _state;
|
||||
private CancellationTokenSource _remoteSyncCancellationTokenSource;
|
||||
|
||||
public PlayerService()
|
||||
{
|
||||
@ -61,7 +62,7 @@ namespace Aurora.Services.Player
|
||||
{
|
||||
get
|
||||
{
|
||||
return _mediaPlayer.Position;
|
||||
return _mediaPlayer != null ? _mediaPlayer.Position : -1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -69,7 +70,7 @@ namespace Aurora.Services.Player
|
||||
{
|
||||
get
|
||||
{
|
||||
return _mediaPlayer.Length;
|
||||
return _mediaPlayer != null ? _mediaPlayer.Length : -1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -105,6 +106,13 @@ namespace Aurora.Services.Player
|
||||
PlaybackState oldState = _state;
|
||||
_state = PlaybackState.Playing;
|
||||
|
||||
//Cancel sync if not cancelled
|
||||
if (_remoteSyncCancellationTokenSource != null && !_remoteSyncCancellationTokenSource.IsCancellationRequested)
|
||||
{
|
||||
_remoteSyncCancellationTokenSource.Cancel();
|
||||
_remoteSyncCancellationTokenSource = null;
|
||||
}
|
||||
|
||||
_mediaPlayer.Play();
|
||||
//Use sync RPC for remote audio
|
||||
if (_currentMedia is RemoteAudio)
|
||||
@ -118,13 +126,13 @@ namespace Aurora.Services.Player
|
||||
//Task completes when host stops syncing (when a song is complete)
|
||||
Task syncTask = new Task(async () =>
|
||||
{
|
||||
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
|
||||
_remoteSyncCancellationTokenSource = new CancellationTokenSource();
|
||||
using (AsyncServerStreamingCall<Sync> syncStream = remotePartyServiceClient
|
||||
.SyncMedia(new SyncMediaRequest() { }))
|
||||
{
|
||||
try
|
||||
{
|
||||
while (await syncStream.ResponseStream.MoveNext(cancellationTokenSource.Token))
|
||||
while (await syncStream.ResponseStream.MoveNext(_remoteSyncCancellationTokenSource.Token))
|
||||
{
|
||||
Sync sync = new Sync(syncStream.ResponseStream.Current);
|
||||
if (sync != null)
|
||||
@ -159,7 +167,7 @@ namespace Aurora.Services.Player
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Exception caught while attempting to sync: " + ex.Message);
|
||||
Console.WriteLine("Exception caught while attempting to sync: " + ex.Message + ": " + ex.StackTrace);
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -198,6 +206,13 @@ namespace Aurora.Services.Player
|
||||
_state = PlaybackState.Stopped;
|
||||
_mediaPlayer.Stop();
|
||||
|
||||
//Cancel sync if not cancelled
|
||||
if (_remoteSyncCancellationTokenSource != null && !_remoteSyncCancellationTokenSource.IsCancellationRequested)
|
||||
{
|
||||
_remoteSyncCancellationTokenSource.Cancel();
|
||||
_remoteSyncCancellationTokenSource = null;
|
||||
}
|
||||
|
||||
if (PlaybackStateChanged != null)
|
||||
{
|
||||
PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(oldState, _state));
|
||||
@ -219,6 +234,10 @@ namespace Aurora.Services.Player
|
||||
/// </summary>
|
||||
private void Unload()
|
||||
{
|
||||
if (_currentMedia == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_currentMedia.Unload();
|
||||
_currentMedia = null;
|
||||
_mediaPlayer.Media = null;
|
||||
|
Reference in New Issue
Block a user