我收到音频流:
public static Task<Stream> DownloadFile(Uri url)
{
var tcs = new TaskCompletionSource<Stream>();
var wc = new WebClient();
wc.OpenReadCompleted += (s, e) =>
{
if (e.Error != null) tcs.TrySetException(e.Error);
else if (e.Cancelled) tcs.TrySetCanceled();
else tcs.TrySetResult(e.Result);
};
wc.OpenReadAsync(url);
return tcs.Task;
}
我把它保存到IsoStorage:
public static void SaveStreamToFile(Stream streamToRead, string fileName)
{
var file = IsolatedStorageFile.GetUserStoreForApplication();
using (var stream = new IsolatedStorageFileStream(fileName, FileMode.Create, file))
{
byte[] buffer = new byte[1024];
while (streamToRead.Read(buffer, 0, buffer.Length) > 0)
{
stream.Write(buffer, 0, buffer.Length);
}
}
}
我将有关播放列表的序列化信息再次保存到IsoStorage,在后台音频播放器中我将其反序列化并获取有关曲目的信息。一切似乎都很好。我在AudioPlayer中的playList属性中获取了所有内容。
问题是当我试图播放曲目时。当我点击下一个曲目按钮时更准确。问题是下一首歌并不总是开始播放。当我添加断点时,例如
方法OnPlayStateChanged(...)
:
case PlayState.Stopped:
break; <-- breakpoint is here
我没有做任何事情。我只是继续播放,之后的每个曲目都在播放。但如果没有这一点,并不是每个曲目都在播放(有些曲目没有开始播放,只有沉默)。我知道这与那个州没有关系(或者我认为)。我认为问题可能在于缓冲。当我在某个地方放置断点时,手机可以在后台做一些事情,然后当我点击继续它就准备好了。我该怎么办?感谢
修改
SimpleTrack类
public class TrackSimple
{
public string Title { get; set; }
public string Artist { get; set; }
public string Album { get; set; }
public string TrackLink { get; set; }
public string CoverUri { get; set; }
public TrackSimple()
{
}
public TrackSimple(string title, string artist, string album, string trackLink, string coverUri)
{
Title = title;
Artist = artist;
Album = album;
TrackLink = trackLink;
CoverUri = coverUri;
}
}
PlayList类
public class PlayList
{
public List<TrackSimple> Tracks { get; set; }
public PlayList()
{
Tracks = new List<TrackSimple>();
}
}