In my Windows Phone 8.1 App, I am using Media Element. I want it to continue to play Audio even if user navigated away from app. MediaElement is using video from remote source (.mp4) file. I also tried with sample video in here; http://go.microsoft.com/fwlink/p/?LinkID=272585
I followed example in How to play audio in the background (XAML) but could not make it work. This example is specific to Windows 8.1 not Windows Phone.
While MediaElement is playing Video Clip when I press Windows button audio stops and when I hit back it continues to work.
My code is like below;
<MediaElement x:Name="MediaElement" VerticalAlignment="Top"
HorizontalAlignment="Stretch"
AudioCategory="BackgroundCapableMedia"
MediaEnded="MediaElement_MediaEnded"
MediaFailed="MediaElement_MediaFailed"
MediaOpened="MediaElement_MediaOpened" SeekCompleted="MediaElement_SeekCompleted"
DownloadProgressChanged="MediaElement_OnDownloadProgressChanged" BufferingProgressChanged="MediaElement_BufferingProgressChanged"
AreTransportControlsEnabled="True" CurrentStateChanged="MediaElement_CurrentStateChanged" />
I have also defined Audio as Supported task Types in Package.appxmanifest
public VideoPlayer()
{
InitializeComponent();
#region SystemMediaTransportControls
// Hook up app to system transport controls.
systemControls = SystemMediaTransportControls.GetForCurrentView();
systemControls.ButtonPressed += SystemControls_ButtonPressed;
// Register to handle the following system transpot control buttons.
systemControls.IsPlayEnabled = true;
systemControls.IsPauseEnabled = true;
systemControls.IsStopEnabled = true;
systemControls.IsEnabled = true;
#endregion
_navigationHelper = new NavigationHelper(this);
_navigationHelper.LoadState += NavigationHelper_LoadState;
_navigationHelper.SaveState += NavigationHelper_SaveState;
}
void SystemControls_ButtonPressed(SystemMediaTransportControls sender, SystemMediaTransportControlsButtonPressedEventArgs args)
{
switch (args.Button)
{
case SystemMediaTransportControlsButton.Play:
PlayMedia();
break;
case SystemMediaTransportControlsButton.Stop:
StopMedia();
break;
case SystemMediaTransportControlsButton.Pause:
PauseMedia();
break;
default:
break;
}
}
private async void StopMedia()
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
MediaElement.Stop();
});
}
async void PlayMedia()
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
MediaElement.Play();
});
}
async void PauseMedia()
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
MediaElement.Pause();
});
}
private void MediaElement_CurrentStateChanged(object sender, RoutedEventArgs e)
{
if (Debugger.IsAttached)
{
Debug.WriteLine("MediaElement.CurrentState: " + MediaElement.CurrentState);
}
switch (MediaElement.CurrentState)
{
case MediaElementState.Playing:
systemControls.PlaybackStatus = MediaPlaybackStatus.Playing;
break;
case MediaElementState.Paused:
systemControls.PlaybackStatus = MediaPlaybackStatus.Paused;
break;
case MediaElementState.Stopped:
systemControls.PlaybackStatus = MediaPlaybackStatus.Stopped;
break;
case MediaElementState.Closed:
systemControls.PlaybackStatus = MediaPlaybackStatus.Closed;
break;
default:
break;
}
}