如何在Windows Phone 7中添加背景音频?

时间:2010-07-13 11:50:03

标签: windows-phone-7

我想在显示图像时添加一些背景音频。

有人可以详细解释一下如何做这个以及一些示例代码吗?

3 个答案:

答案 0 :(得分:5)

Charles Petzold最近发表了关于播放音乐文件的博客。你必须测试它以确定它是否继续播放:

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

如果这不起作用,请参阅Jaime Rodriguez关于在锁定屏幕下运行应用程序的帖子:

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

希望这些链接有所帮助!

答案 1 :(得分:1)

即使使用Silverlight,使用仍然可以引用XNA库。如果您这样做,可以使用SoundEffect课程播放音乐:

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

答案 2 :(得分:1)

您甚至不需要添加任何XNA库,因为Silverlight具有可以在代码隐藏中使用的内置MediaElement(LayoutRoot是主网格):

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

和XAML:

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