Pretty good audio sync if I do say so myself :)

This commit is contained in:
watsonb8
2019-11-29 22:04:55 -05:00
parent 8bea1d03da
commit 41e853b1c6
7 changed files with 25 additions and 14 deletions

View File

@ -10,8 +10,10 @@ using LibVLCSharp.Shared;
namespace Aurora.Services.PlayerService
{
public class PlayerService : BaseService<PlayerService>
{
private const long _ticksPerMillisecond = 10000;
private BaseMedia _currentMedia;
private MediaPlayer _mediaPlayer;
private LibVLC _libvlc;
@ -134,10 +136,10 @@ namespace Aurora.Services.PlayerService
DateTime localTime = time.DateTime;
//Get offset - elapsed time converted to milliseconds
float offset = (((localTime.Ticks - sync.ServerTime) * 100) / (1000 * 1000));
float length = CurrentMediaLength;
float newPosition = (sync.TrackTime + (offset + time.Elapsed.Milliseconds)) / length;
float offset = (localTime.Ticks - sync.ServerTimeTicks) * _ticksPerMillisecond;
float newPosition = (sync.TrackPosition + offset);
//Adjust position if greater than 10 percent difference
float oldPosition = _mediaPlayer.Position;
@ -145,7 +147,15 @@ namespace Aurora.Services.PlayerService
newPosition - oldPosition < -0.001)
{
_mediaPlayer.Position = newPosition;
Console.WriteLine(string.Format("Audio synced: oldPosition: {0} newPosition: {1}", oldPosition, newPosition));
Console.WriteLine(string.Format("**Audio synced**"));
Console.WriteLine(string.Format("Remote Server Time {0}", new DateTime(sync.ServerTimeTicks).ToLongTimeString()));
Console.WriteLine(string.Format("Remote Track Time: {0}", sync.TrackPosition));
Console.WriteLine(string.Format("Local Server Time: {0}", time.DateTime.ToLongTimeString()));
Console.WriteLine(string.Format("Local Track Time: {0}", _mediaPlayer.Position));
Console.WriteLine(string.Format("Offset: {0}", offset));
Console.WriteLine(string.Format("Old Position: {0}", oldPosition));
Console.WriteLine(string.Format("New Position: {0}", newPosition));
}
}
}
@ -158,7 +168,6 @@ namespace Aurora.Services.PlayerService
});
syncTask.Start();
// Task syncTask = Task.Run(() => Sync(remoteSyncClient));
}
}