Silverlight Windows Phone 8 mp3播放问题
我正试图在我的wp 8应用程序中播放mp3,我想我忘了什么,你能帮我吗 我的简化代码如下所示: 在page.xaml.cs中,代码为:Silverlight Windows Phone 8 mp3播放问题,silverlight,windows-phone-8,windows-phone,Silverlight,Windows Phone 8,Windows Phone,我正试图在我的wp 8应用程序中播放mp3,我想我忘了什么,你能帮我吗 我的简化代码如下所示: 在page.xaml.cs中,代码为: public void Play(object sender, RoutedEventArgs e) { if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState) {
public void Play(object sender, RoutedEventArgs e)
{
if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState)
{
BackgroundAudioPlayer.Instance.Pause();
}
else
{
BackgroundAudioPlayer.Instance.Play();
}
}
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);
}
}
}
}
}
}
App.xaml.cs中的代码为:
public void Play(object sender, RoutedEventArgs e)
{
if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState)
{
BackgroundAudioPlayer.Instance.Pause();
}
else
{
BackgroundAudioPlayer.Instance.Play();
}
}
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
状态从未改变,但我不明白为什么(播放功能被触发)你需要告诉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();
你在哪里告诉它播放什么曲目/文件?我通常使用SoundEffect.Stream控件,我对它不熟悉