Windows Store Services Using Background Audio with MediaElement

In my Windows Phone 8.1 application, I am using Media Element. I want it to continue playing audio, even if the user has switched from the application. MediaElement uses video from a remote source file (.mp4). I also tried with a sample video here; http://go.microsoft.com/fwlink/p/?LinkID=272585

I followed suit in How to play audio in the background (XAML) , but couldn't get it working. This example applies only to Windows 8.1, not to Windows Phone.

While MediaElement plays the video clip, when I press the Windows button, the sound stops, and when I click it, it continues to work.

My code is as follows:

<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 also defined Audio as the 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; } } 
+1
source share
1 answer

Windows Phone does not use the same mechanism for Background Sound as Windows, primarily because cheap phones do not have enough resources to run two applications at the same time.

Instead, Windows Phone uses a dedicated background process to play music in the background. For more information on how to do this in a Windows Runtime application, see BackgroundMediaPlayer .

+1
source

Source: https://habr.com/ru/post/944046/


All Articles