Sravanti
Sravanti

Reputation: 1407

How To add Background audio in windows phone 7?

I would like to add some background audio while an image is being displayed.

Can someone please give me a detailed explanation on how to do this and some sample code?

Upvotes: 0

Views: 1679

Answers (3)

Den
Den

Reputation: 16826

You don't even need to add any XNA libraries because Silverlight has the built-in MediaElement that can be used both from code-behind (LayoutRoot is the main grid):

MediaElement element = new MediaElement();
element.Source = new Uri("sound.mp3",UriKind.Relative);
LayoutRoot.Children.Add(element);
element.Play();

and XAML:

<MediaElement Source="sound.mp3" AutoPlay="True"></MediaElement>

Upvotes: 1

mattStroshane
mattStroshane

Reputation: 221

Charles Petzold recently blogged about playing music files. You'll have to test it to see if it stays playing:

http://www.charlespetzold.com/blog/2010/11/Playing-Music-Files-on-WP7.html

If that doesn't work, see Jaime Rodriguez's post about running apps under the lock screen:

_http://blogs.msdn.com/b/jaimer/archive/2010/11/01/running-a-windows-phone-application-under-the-lock-screen.aspx

Hope these links help!

Upvotes: 5

Matt Lacey
Matt Lacey

Reputation: 65564

Even if using Silverlight, use can still reference the XNA libraries. If you do this, you can use the SoundEffect class to play music:

Uri uri = new Uri("file.wav", UriKind.Relative);
StreamResourceInfo sri = Application.GetResourceStream(uri);
SoundEffect effect = SoundEffect.FromStream(sri.Stream);
effect.Play();

Upvotes: 1

Related Questions