First pass at syncing worked with some bug fixes
This commit is contained in:
@ -8,10 +8,12 @@ namespace Aurora.Services.PlayerService
|
||||
public class MediaChangedEventArgs : EventArgs
|
||||
{
|
||||
public BaseMetadata NewMetadata { get; private set; }
|
||||
public string NewId { get; private set; }
|
||||
|
||||
public MediaChangedEventArgs(BaseMetadata metadata)
|
||||
public MediaChangedEventArgs(string id, BaseMetadata metadata)
|
||||
{
|
||||
NewMetadata = metadata;
|
||||
NewId = id;
|
||||
}
|
||||
}
|
||||
}
|
@ -41,7 +41,7 @@ namespace Aurora.Services.PlayerService
|
||||
{
|
||||
get
|
||||
{
|
||||
return this._currentMedia == null;
|
||||
return this._currentMedia != null;
|
||||
}
|
||||
}
|
||||
|
||||
@ -50,7 +50,12 @@ namespace Aurora.Services.PlayerService
|
||||
return _currentMedia == media;
|
||||
}
|
||||
|
||||
public float CurrentMediaTime
|
||||
public BaseMedia CurrentMedia
|
||||
{
|
||||
get { return _currentMedia; }
|
||||
}
|
||||
|
||||
public float CurrentMediaPosition
|
||||
{
|
||||
get
|
||||
{
|
||||
@ -85,7 +90,7 @@ namespace Aurora.Services.PlayerService
|
||||
|
||||
if (MediaChanged != null)
|
||||
{
|
||||
MediaChanged.Invoke(this, new MediaChangedEventArgs(_currentMedia.Metadata));
|
||||
MediaChanged.Invoke(this, new MediaChangedEventArgs(_currentMedia.Id, _currentMedia.Metadata));
|
||||
}
|
||||
|
||||
}
|
||||
@ -103,43 +108,53 @@ namespace Aurora.Services.PlayerService
|
||||
if (_currentMedia is RemoteAudio)
|
||||
{
|
||||
RemoteAudio media = _currentMedia as RemoteAudio;
|
||||
RemoteSyncService.RemoteSyncServiceClient _remoteSyncClient = media.RemoteSyncClient;
|
||||
|
||||
//Sync playback in a separate task
|
||||
//Task completes when host stops syncing (when a song is complete)
|
||||
Task syncTask = new Task(async () =>
|
||||
if (!media.FromHost)
|
||||
{
|
||||
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
|
||||
using (AsyncServerStreamingCall<Sync> syncStream = _remoteSyncClient
|
||||
.GetMediaSync(new Proto.General.Empty()))
|
||||
RemoteSyncService.RemoteSyncServiceClient _remoteSyncClient = media.RemoteSyncClient;
|
||||
|
||||
//Sync playback in a separate task
|
||||
//Task completes when host stops syncing (when a song is complete)
|
||||
Task syncTask = new Task(async () =>
|
||||
{
|
||||
try
|
||||
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
|
||||
using (AsyncServerStreamingCall<Sync> syncStream = _remoteSyncClient
|
||||
.GetMediaSync(new Proto.General.Empty()))
|
||||
{
|
||||
while (await syncStream.ResponseStream.MoveNext(cancellationTokenSource.Token))
|
||||
try
|
||||
{
|
||||
Sync sync = new Sync(syncStream.ResponseStream.Current);
|
||||
if (sync != null)
|
||||
while (await syncStream.ResponseStream.MoveNext(cancellationTokenSource.Token))
|
||||
{
|
||||
//Adjust position based on sync
|
||||
DateTime localTime = Utils.TimeUtils.GetNetworkTime();
|
||||
//Get offset converted to milliseconds
|
||||
float offset = ((localTime.Ticks - sync.ServerTime) * 100) / (1000 * 1000);
|
||||
Sync sync = new Sync(syncStream.ResponseStream.Current);
|
||||
if (sync != null)
|
||||
{
|
||||
//Adjust position based on sync
|
||||
DateTime localTime = Utils.TimeUtils.GetNetworkTime();
|
||||
//Get offset converted to milliseconds
|
||||
float offset = ((localTime.Ticks - sync.ServerTime) * 100) / (1000 * 1000);
|
||||
|
||||
float length = CurrentMediaLength;
|
||||
float position = (sync.TrackTime + offset) / length;
|
||||
float length = CurrentMediaLength;
|
||||
float newPosition = (sync.TrackTime + offset) / length;
|
||||
|
||||
_mediaPlayer.Position = position;
|
||||
//Adjust position if greater than 10 percent difference
|
||||
float oldPosition = _mediaPlayer.Position;
|
||||
if (newPosition - oldPosition > 0.001 ||
|
||||
newPosition - oldPosition < -0.001)
|
||||
{
|
||||
_mediaPlayer.Position = newPosition;
|
||||
Console.WriteLine("Audio synced");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Exception caught while attempting to sync: " + ex.Message);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("Exception caught while attempting to sync: " + ex.Message);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
syncTask.Start();
|
||||
syncTask.Start();
|
||||
}
|
||||
}
|
||||
|
||||
if (PlaybackStateChanged != null)
|
||||
|
Reference in New Issue
Block a user