How to switch from MediaElement to BackgroundMediaPlayer to the application lose focus

I have a music player page like PlayMusic.xaml and it uses the MediaPlayer element to play music.

I want to toggle BackgroundMediaPlayer whenever the user clicks the back button or the windows or lock screen button.

I would also like to continue where the song will remain. (How BackgroundMediaPlayer should continue from the 30th second)

Is there any mechanism on a Windows phone when ever, when I lose focus with MediaPlayer , can I switch to BackgroundMediaPlayer and continue playing music?

Note. I can use BackgroundMediaPlayer directly, then it always plays music, but in this case I cannot use MediaPlayer in PlayMusic.xaml, which makes it possible to watch video clips, full-screen and look for controls.

+1
source share
2 answers

There is no specific mechanism for this that I know of. You can build it yourself, but it's not easy:

  • Firstly, you need to have a background background job and know how to communicate with it (this is not just in itself, this example helped me a bit).
  • When you start playing anything in MediaPlayer , the background audio download is canceled. When you use it again, its Run method is called again in the same process. Because of this, the background task must properly manage its life cycle, i.e. You need to subscribe to events and get a delay in the Run method and unsubscribe from events, call BackgroundMediaPlayer.Shutdown() and release the delay in the Cancel event. Also, I found that after restarting the task, I need to subscribe to BackgroundMediaPlayer.MessageReceivedFromBackground in the foreground process, because restarting seems to clear it.
  • You may find that MediaPlayer β€œlost focus” (and start background playback) when the page moves. However, this will not cover the case of multitasking a user away from your application or the case of turning off the screen. You can catch them using the CoreWindow.GetForCurrentThread().Activated event.
  • When you switch to background sound, you need to transfer the current position. This can be done using messaging ( BackgroundMediaPlayer.SendMessageToBackground ).
  • You can start background playback at the specified position by turning AutoPlay off and setting the position after MediaOpened .

Hope this helps. I'm still struggling with this (this is one gigantic trial and error process), but it seems to work.

+2
source

In your package manifest, you need to go to the Declaration tab and select Background Tasks , and then select Audio , and also select an entry point for your applications.

Inside your xaml

 <MediaElement AutoPlay="True" AreTransportControlsEnabled="True" x:Name="musicPlayer" Source="Assets/video1.mp4" AudioCategory="BackgroundCapableMedia" CurrentStateChanged="MusicPlayer_CurrentStateChanged"/> 

Code for C #

 SystemMediaTransportControls systemControls; public BlankPage7() { this.InitializeComponent(); // 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; } private void SystemControls_ButtonPressed(SystemMediaTransportControls sender, SystemMediaTransportControlsButtonPressedEventArgs args) { switch (args.Button) { case SystemMediaTransportControlsButton.Play: PlayMedia(); break; case SystemMediaTransportControlsButton.Pause: PauseMedia(); break; default: break; } } private void MusicPlayer_CurrentStateChanged(object sender, RoutedEventArgs e) { switch (musicPlayer.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; } } async void PlayMedia() { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { musicPlayer.Play(); }); } async void PauseMedia() { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { musicPlayer.Pause(); }); } protected override void OnNavigatedFrom(NavigationEventArgs e) { if (systemControls != null) { systemControls.ButtonPressed -= SystemControls_ButtonPressed; systemControls.IsPlayEnabled = false; systemControls.IsPauseEnabled = false; systemControls.PlaybackStatus = MediaPlaybackStatus.Closed; } } 

This, I hope, will solve your goal ... For more information, you can contact MSDN Link.

0
source

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


All Articles