In many applications, we use the MediaElement control to play music videos. If the external player is playing music, when the application has the MediaElement control, the playing concert stops, this is mainly because the external player and MediaElement share the playing hardware resources of the mobile phone. If we exit or the current application of the video, the external player will not be restored. To solve this problem, we can use the following solutions.
First, you need to process four events in App. xaml. cs:
- private void Application_Launching(object sender, LaunchingEventArgs e)
- {
- FrameworkDispatcher.Update();
- if (MediaPlayer.State == MediaState.Playing)
- {
- MediaPlayer.Pause();
- }
- }
- private void Application_Activated(object sender, ActivatedEventArgs e)
- {
- if (MediaPlayer.State == MediaState.Playing)
- {
- MediaPlayer.Pause();
- }
- }
- private void Application_Deactivated(object sender, DeactivatedEventArgs e)
- {
- if (MediaPlayer.State == MediaState.Paused || MediaState.Stopped == MediaPlayer.State)
- {
- MediaPlayer.Resume();
- }
- }
- private void Application_Closing(object sender, ClosingEventArgs e)
- {
- if (MediaPlayer.State == MediaState.Paused||MediaState.Stopped==MediaPlayer.State)
- {
- MediaPlayer.Resume();
- }
- }
Stop the external player in the loading and recovery events, and resume the external player pause in the closing and snowy events respectively.
The most critical point is that after the MediaElement control is restored, it will lose the value of the Source attribute. Therefore, we need to set the MediaElement before the control in the control page. source is stored, and the stored value is assigned back after restoration. The Code is as follows:
- protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
- {
- PhoneApplicationService.Current.State.Clear();
- if (shengyin_ME.Source != null)
- {
- PhoneApplicationService.Current.State.Add("URL", shengyin_ME.Source);
- }
- base.OnNavigatedFrom(e);
- }
-
- protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
- {
-
- if (PhoneApplicationService.Current.State.Keys.Contains("URL"))
- {
- shengyin_ME.Source = PhoneApplicationService.Current.State["URL"] as Uri;
- }
- base.OnNavigatedTo(e);
- }
In this way, the application or the application can be closed, and the external player will continue playing.
This article is from "Gui Su Wei" blog, please be sure to keep this source http://axzxs.blog.51cto.com/730810/788923