开发者

Working with Windows Phone 7, how do I create a global MediaElement?

开发者 https://www.devze.com 2023-02-06 07:08 出处:网络
I need to create a MediaEle开发者_如何学Cment that will keep playing when I navigate to different pages within my app. Any ideas on how to do this?I actually just found a solution. As so often before

I need to create a MediaEle开发者_如何学Cment that will keep playing when I navigate to different pages within my app. Any ideas on how to do this?


I actually just found a solution. As so often before once you write down a question you get an idea that works yourself :)

private MediaElement _mediaElement;

...

_mediaElement = new MediaElement {Volume = 1, AutoPlay = false};
var _pop = new Popup {Child = _mediaElement, IsOpen = true};

This works - in case anybody else runs into the same problem...


I don't think that is possible.

MediaElement is a UIElement which must be hosted inside a page to work.

Each time you navigate, a new page instance is loaded in WP7. A really ugly hack might work, but I don't recommend it.

What are you trying to do?

You could remember the position it was at and restart it when back on page.


If you want to have music that will play throughout an application, consider using the MediaPlayer class.


Alternatively you re-template the PhoneApplicationFrame and put it there. I'm using this solution in my app and I like it better than the popup solution because you can layout the mediaelement along with everything else (like the appbar) if you want controls and stuff.


I had a similar problem and I post on my blog a soltuion.

Silverlight for Windows Phone 7 is based on the page navigation system. Key point is the lifetime of pages – each page is deleted while you navigate to another. To have an object - a singleton for example - during the whole application life it could be placed in App class (App.xaml.cs file). MediaElement is definitely an object that should be one (there must be only one music player). The best and simple solution is place the MediaElement in XAML(it needs to be a part of visual tree), I have used application resources:

<!--Application Resources-->
    <Application.Resources>
       <MediaElement x:Name="mediaPlayer" Source="/Sound/horrorSong.mp3" AutoPlay="False"  />
    </Application.Resources>

Then to get it from any page use resources as a dictionary:

MediaElement player = null; // get the media element from App resources
if (App.Current.Resources.Contains("mediaPlayer"))
{
   player = App.Current.Resources["mediaPlayer"] as MediaElement;
}
if (player != null)
{
   player.Play();
}

It is worth notice that MediaElement can’t play the music while Zune is connect. It is a good idea to prompt the user about it. In a method

if (NetworkInterface.GetIsNetworkAvailable())
{
   if (NetworkInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
   {
          zuneTextBlock.Visibility = System.Windows.Visibility.Visible;
          return;
   }
}
    zuneTextBlock.Visibility = System.Windows.Visibility.Collapsed;

This code does not guarantee if Zune is on (User still could have the phone plugged) but is very likely that Zune is running(Zune starts while phone is connecting).


I found this solution: http://www.jayway.com/2010/10/04/enable-background-audio-for-multiple-pages-in-windows-phone-7/

Works on WP8.

0

精彩评论

暂无评论...
验证码 换一张
取 消