WP7 uses MediaElement in the shadow

Source: Internet
Author: User

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:
 
 
  1. private void Application_Launching(object sender, LaunchingEventArgs e)  
  2.         {  
  3.             FrameworkDispatcher.Update();  
  4.             if (MediaPlayer.State == MediaState.Playing)  
  5.             {                
  6.                 MediaPlayer.Pause();  
  7.             }  
  8.         }          
  9.         private void Application_Activated(object sender, ActivatedEventArgs e)  
  10.         {  
  11.             if (MediaPlayer.State == MediaState.Playing)  
  12.             {  
  13.                 MediaPlayer.Pause();  
  14.             }  
  15.         }       
  16.         private void Application_Deactivated(object sender, DeactivatedEventArgs e)  
  17.         {  
  18.             if (MediaPlayer.State == MediaState.Paused || MediaState.Stopped == MediaPlayer.State)  
  19.             {  
  20.                 MediaPlayer.Resume();  
  21.             }  
  22.         }  
  23.         private void Application_Closing(object sender, ClosingEventArgs e)  
  24.         {  
  25.             if (MediaPlayer.State == MediaState.Paused||MediaState.Stopped==MediaPlayer.State)  
  26.             {  
  27.                 MediaPlayer.Resume();  
  28.             }  
  29.         }  

 

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:

 

 
 
  1. protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)  
  2.         {  
  3.             PhoneApplicationService.Current.State.Clear();  
  4.             if (shengyin_ME.Source != null)  
  5.             {  
  6.                 PhoneApplicationService.Current.State.Add("URL", shengyin_ME.Source);  
  7.             }  
  8.             base.OnNavigatedFrom(e);  
  9.         }  
  10.         
  11.         protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)  
  12.         {  
  13.          
  14.             if (PhoneApplicationService.Current.State.Keys.Contains("URL"))  
  15.             {  
  16.                 shengyin_ME.Source = PhoneApplicationService.Current.State["URL"] as Uri;  
  17.             }  
  18.             base.OnNavigatedTo(e);  
  19.         }  

 

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.