我在WP7中实现了一个工作音频播放器,它通过HttpWebRequest在线获取音频流,并使用AudioStreamingAgent和MediaStreamSource在后台播放。
获取流的代码如下:
public class AudioTrackStreamer : AudioStreamingAgent
{
protected override void OnBeginStreaming(AudioTrack track, AudioStreamer streamer)
{
stream = GetDownloadStream(track.Tag);
// Use sine wave audio generator to simulate a streaming audio feed
BackgroundMp3MediaStreamSource mss = new BackgroundMp3MediaStreamSource(stream);
// Event handler for when a track is complete or the user switches tracks
mss.StreamingCompleted += new EventHandler(mss_StreamingCompleted);
// Set the source
streamer.SetSource((MediaStreamSource) mss);
}
}
我需要更改客户端以获取AES编码流并在播放之前对其进行解码。
我虽然可以创建解码流并将其传递给BackgroundMp3MediaStreamSource。类似的东西:
...
stream = GetDownloadStream(track.Tag);
decodedStream = GetDecodedStream(stream);
BackgroundMp3MediaStreamSource mss = new BackgroundMp3MediaStreamSource(decodedStream);
...
private Stream GetDecodedStream(Stream encoded) {
Stream destination = new MemoryStream();
BackgroundWorker streamCopier = new BackgroundWorker();
streamCopier.DoWork += (o, e) => StreamCopierDoWork(encoded, destination);
streamCopier.RunWorkerAsync();
return new NonUiStreamer(destination);
}
private static void StreamCopierDoWork(Stream encoded, Stream destination) {
const int chunk = 1 * 1024;
var buffer = new byte[chunk];
AsyncCallback rc = null;
rc = readResult => {
int read = encoded.EndRead(readResult);
if (read > 0) {
destination.BeginWrite(buffer, 0, read, writeResult => {
destination.EndWrite(writeResult);
encoded.BeginRead(buffer, 0, buffer.Length, rc, null);
}, null);
}
};
encoded.BeginRead(buffer, 0, chunk, rc, null);
}
注意:我还没有解码流。只需将其异步复制到另一个流,看看是否一切正常。
但是当我尝试读取解码流decodedStream.Read(data, 0, 3);
的第一个字节时,它返回0个字节。
为什么我不能读取decodeStream?