我想在 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/