c# - 在 Windows Phone 后台播放音频

标签 c# windows-phone-7 windows-runtime windows-phone

我想在 Windows Phone 的后台播放一些音频。我已经编写了一些代码,例如来自 Microsoft ( http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202978(v=vs.105).aspx ) 的示例,但在我的应用程序上,用户有机会选择后台代理必须播放的 uri。但我不知道如何将应用程序中的音轨元素设置为后台代理的音轨元素。

我已经在我的代理中尝试了以下代码:

private static AudioTrack _streamTrack;
public static AudioTrack StreamTrack { get { return _streamTrack; } set { _streamTrack = value; } }

并尝试在我的应用中设置此变量,例如:

AudioPlayer.StreamTrack = new AudioTrack(new Uri(stream.StreamUri, UriKind.Absolute), stream.StreamName, stream.StreamGenre, stream.StreamGenre, null);

但它不起作用。我该如何解决这个问题?

最佳答案

完成此操作的一种方法是使用 XNA 库

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

然后声明你的音效

SoundEffect _BGMUSIC;

我用的就是这种加载音效的方法

 //Put this in your main method
 LoadSound("sfx/piano.wav", out _BGMUSIC);


 //put this method in the same class
 private void LoadSound(String SoundFilePath, out SoundEffect Sound)
        {
            // For error checking, assume we'll fail to load the file.
            Sound = null;

            try
            {
                // Holds informations about a file stream.
                StreamResourceInfo SoundFileInfo = App.GetResourceStream(new Uri(SoundFilePath, UriKind.Relative));

                // Create the SoundEffect from the Stream
                Sound = SoundEffect.FromStream(SoundFileInfo.Stream);
                FrameworkDispatcher.Update();
            }
            catch (NullReferenceException)
            {
                // Display an error message
                MessageBox.Show("Couldn't load sound " + SoundFilePath);
            }
        }

终于可以播放音效了

 _BGMUSIC.Play();

关于c# - 在 Windows Phone 后台播放音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15324713/

相关文章:

c# - asp.net中不同时区的日期随机变化

windows-phone-7 - Core Data 相当于 Windows Phone

c# - 为什么将 XAML 图像源设置为 URI 比使用 HttpClient 获取图像更快?

windows-8 - Windows 8 应用程序 : How to pass a parameter on GoBack()?

c# - CSC : error CS7038: Failed to emit module

c# - 通过 .net 使用 Windows 资源管理器进行其他文件存储

c# - 未达到 await 语句后的代码

windows-phone-7 - 在 wp7 中找到 IMEI 号?

xaml - 访问文本 block 的背景色

c# - 如何将 .csv 文件的每一行分隔成一个字符串列表>