我使用HttpWebRequest和WebClient Both在我的应用程序中下载了一个.mp3歌曲。
我正在使用.mp3扩展名文件名保存隔离存储中的响应流。
当我试图用BackgroundAudioPlayer
播放它时,它不会播放。
这是我的代码。请说明它有什么问题。
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.FileExists(lstBoxSongList.SelectedItem as string))
{
using (IsolatedStorageFileStream isfStream = store.OpenFile(lstBoxSongList.SelectedItem.ToString(), FileMode.Open, FileAccess.Read))
{
AudioTrack track = new AudioTrack(new Uri(lstBoxSongList.SelectedItem as string, UriKind.RelativeOrAbsolute), lstBoxSongList.SelectedItem.ToString(), "", "", null);
BackgroundAudioPlayer.Instance.Track = track;
BackgroundAudioPlayer.Instance.Volume = 1;
BackgroundAudioPlayer.Instance.Play();
}
}
}
下面给出了将下载的流保存到IsolatedStorage
的方式。
webClient.OpenReadCompleted += (s1, e1) =>
{
if (e1.Error == null)
{
bool isSpaceAvailable = IsSpaceIsAvailable(e1.Result.Length);
if (isSpaceAvailable)
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
// Save mp3 to Isolated Storage
if (store.FileExists(filename))
{
store.DeleteFile(filename);
}
using (var isfs = new IsolatedStorageFileStream(filename, FileMode.CreateNew, IsolatedStorageFile.GetUserStoreForApplication()))
{
long fileLen = e1.Result.Length;
byte[] b = new byte[fileLen];
e1.Result.Read(b, 0, b.Length);
isfs.Write(b, 0, b.Length);
isfs.Flush();
}
}
}
}
}
请说明此代码有什么问题。
以下是包含我的文件名的ListBox的Xaml代码。
<ListBox Name="lstBoxSongList" SelectionChanged="lstBoxSongList_SelectionChanged" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>