public BetterMediaPlayer(List<Song> songs)
{
this.songs = songs;
currentSongIndex = 0;
activeSong = songs.ElementAtOrDefault<Song>(currentSongIndex);
haveControl = false;
startTimerCallback = (s, e) =>
{
if (MediaPlayer.State == MediaState.Playing)
{
timer.Change(0, 100);
}
else
{
timer.Change(Timeout.Infinite, Timeout.Infinite);
}
RaisePropertyChanged(StatePropertyName);
};
MediaPlayer.MediaStateChanged += startTimerCallback;
TimeSpan tenthOfASecond = new TimeSpan(0, 0, 0, 0, 1000);
timer = new Timer(
x =>
{
// This is a hack to enable to previous/next buttons to work.
// This will be true if they pressed either button so we will
// always advance to the next song in that case, so the previous
// button will actually go to the next song as well. It's an
// unfortunate limitation for now.
if (MediaPlayer.State != MediaState.Stopped)
{
if (MediaPlayer.PlayPosition.CompareTo(tenthOfASecond) < 0 &&
MediaPlayer.PlayPosition.CompareTo(hiresPlayPosition) < 0)
{
MoveNext();
}
// This is to skip to the next song when we are about to end the
// current song
else if (MediaPlayer.PlayPosition.CompareTo(nextSongTriggerPosition) >= 0)
{
MoveNext();
}
else
{
// Update the PlayPosition and RemainingTime at most once
// a second
if (PlayPosition.Seconds != MediaPlayer.PlayPosition.Seconds)
{
playPosition = MediaPlayer.PlayPosition;
DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
RaisePropertyChanged(PlayPositionPropertyName);
RaisePropertyChanged(RemainingTimePropertyName);
});
}
hiresPlayPosition = MediaPlayer.PlayPosition;
}
}
},
null, Timeout.Infinite, Timeout.Infinite);
}