Skip to content

Commit

Permalink
Merge pull request Baseflow#216 from BtrJay/uwp_resume_fix
Browse files Browse the repository at this point in the history
Fix Resume issue on UWP
  • Loading branch information
martijn00 authored Jul 14, 2017
2 parents c624d8e + 0c9a368 commit bf36cca
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
45 changes: 31 additions & 14 deletions MediaManager.UWP/AudioPlayerImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,19 @@ public AudioPlayerImplementation(IVolumeManager volumeManager)
};

_player.MediaEnded += (sender, args) => { MediaFinished?.Invoke(this, new MediaFinishedEventArgs(_currentMediaFile)); };

_player.PlaybackSession.BufferingStarted += (sender, args) =>
{
var bufferedTime =
TimeSpan.FromSeconds(sender.BufferingProgress *
sender.NaturalDuration.TotalSeconds);
BufferingChanged?.Invoke(this,
new BufferingChangedEventArgs(sender.BufferingProgress, bufferedTime));
};

_player.PlaybackSession.BufferingProgressChanged += (sender, args) =>
{
//This seems not to be fired at all
var bufferedTime =
TimeSpan.FromSeconds(_player.PlaybackSession.BufferingProgress*
_player.PlaybackSession.NaturalDuration.TotalSeconds);
Expand Down Expand Up @@ -149,20 +160,33 @@ public async Task PlayPause()
await Pause();
}

public async Task Play(IMediaFile mediaFile)
public async Task Play(IMediaFile mediaFile = null)
{
try
{
var mediaPlaybackList = new MediaPlaybackList();
var mediaSource = await CreateMediaSource(mediaFile);
var item = new MediaPlaybackItem(mediaSource);
mediaPlaybackList.Items.Add(item);
_player.Source = mediaPlaybackList;
_player.Play();
var sameMediaFile = mediaFile == null || mediaFile.Equals(_currentMediaFile);
if (Status == MediaPlayerStatus.Paused && sameMediaFile)
{
_player.PlaybackSession.PlaybackRate = 1;
_player.Play();
return;
}

if (mediaFile != null)
{
_currentMediaFile = mediaFile;
var mediaPlaybackList = new MediaPlaybackList();
var mediaSource = await CreateMediaSource(mediaFile);
var item = new MediaPlaybackItem(mediaSource);
mediaPlaybackList.Items.Add(item);
_player.Source = mediaPlaybackList;
_player.Play();
}
}
catch (Exception e)
{
MediaFailed?.Invoke(this, new MediaFailedEventArgs("Unable to start playback", e));
Status = MediaPlayerStatus.Stopped;
}
}

Expand Down Expand Up @@ -199,12 +223,5 @@ private async Task<MediaSource> CreateMediaSource(IMediaFile mediaFile)

return MediaSource.CreateFromUri(new Uri(mediaFile.Url));
}

private Task Play()
{
_player.PlaybackSession.PlaybackRate = 1;
_player.Play();
return Task.CompletedTask;
}
}
}
15 changes: 14 additions & 1 deletion Samples/Simple/MediaSample.UWP/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using Windows.ApplicationModel.Core;
using Windows.UI.Core;
using Windows.UI.Xaml;
Expand All @@ -22,11 +23,19 @@ public MainPage()
{
InitializeComponent();
CrossMediaManager.Current.PlayingChanged += OnPlayingChanged;
CrossMediaManager.Current.BufferingChanged += OnBufferingChanged;
CrossMediaManager.Current.StatusChanged += OnStatusChanged;
CrossMediaManager.Current.VideoPlayer.RenderSurface = VideoCanvas;
CrossMediaManager.Current.MediaFileChanged += CurrentOnMediaFileChanged;
}

private void OnBufferingChanged(object sender, BufferingChangedEventArgs bufferingChangedEventArgs)
{
var bufferingProgress = bufferingChangedEventArgs?.BufferProgress ?? 0;
var bufferingTime = bufferingChangedEventArgs?.BufferedTime;
Debug.WriteLine($"buffering progress: {bufferingProgress}, buffering time: {bufferingTime}");
}

private void CurrentOnMediaFileChanged(object sender, MediaFileChangedEventArgs mediaFileChangedEventArgs)
{
var mediaFile = mediaFileChangedEventArgs.File;
Expand Down Expand Up @@ -85,9 +94,13 @@ private async void OnPlayingChanged(object sender, PlayingChangedEventArgs e)
});
}

private MediaFile mediaFile;
private async void PlayUrl(object sender, RoutedEventArgs e)
{
var mediaFile = new MediaFile("http://www.montemagno.com/sample.mp3", MediaFileType.Audio);
if (mediaFile == null)
{
mediaFile = new MediaFile("http://www.montemagno.com/sample.mp3", MediaFileType.Audio);
}
await CrossMediaManager.Current.Play(mediaFile);
//var file = await KnownFolders.VideosLibrary.GetFileAsync("big_buck_bunny.mp4");
//await CrossMediaManager.Current.Play(file.Path, MediaFileType.VideoFile);
Expand Down

0 comments on commit bf36cca

Please sign in to comment.