我想在我的wp 8应用程序中播放mp3,我想忘记了一些事情,请你帮忙
我的简化代码就是这样:
page.xaml.cs中的代码是:
public void Play(object sender, RoutedEventArgs e)
{
if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState)
{
BackgroundAudioPlayer.Instance.Pause();
}
else
{
BackgroundAudioPlayer.Instance.Play();
}
}
App.xaml.cs代码中的是:
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
string[] files = new string[] { "song.mp3"};
foreach (var _fileName in files)
{
if (!storage.FileExists(_fileName))
{
string _filePath = "Sounds/" + _fileName;
StreamResourceInfo resource = Application.GetResourceStream(new Uri(_filePath, UriKind.Relative));
using (IsolatedStorageFileStream file = storage.CreateFile(_fileName))
{
int chunkSize = 4096;
byte[] bytes = new byte[chunkSize];
int byteCount;
while ((byteCount = resource.Stream.Read(bytes, 0, chunkSize)) > 0)
{
file.Write(bytes, 0, byteCount);
}
}
}
}
}
}
我可以看到我的BackgroundAudioPlayer.Instance
状态永远不会改变,但我无法弄清楚为什么(游戏功能被嘲弄)
答案 0 :(得分:1)
你需要告诉BackgroundAudioPlayer要播放的曲目 类似的东西:
var track = new AudioTrack(
new Uri("/song.mp3", UriKind.Relative),
"song name",
"artist name",
"album name",
null); // no artwork
BackgroundAudioPlayer.Instance.Track = track;
BackgroundAudioPlayer.Instance.Play();