使用CSCore,如何从FileStream或MemoryStream中播放WMA或MP3 (与使用以string作为文件路径或url的方法不同)。
发布于 2016-05-24 13:22:51
由于GetCodec(Stream stream, object key)-overload of CodecFactory-class是内部的,您只需手动和直接选择解码器就可以执行相同的步骤。实际上,CodeFactory只是一个自动确定解码器的助手类,所以如果您已经知道了您的编解码器,您可以自己来做。在内部,当传递文件路径时,CSCore检查文件扩展名,然后打开一个FileStream (使用File.OpenRead),该FileStream将被处理到选定的解码器。
你需要做的就是为你的编解码器使用特定的解码器。
对于MP3,您可以使用从DmoStream继承的DmoMP3Decoder来实现需要处理的IWaveSource-interface作为声源。
以下是Codeplex上文档中的调整样本
public void PlayASound(Stream stream)
{
//Contains the sound to play
using (IWaveSource soundSource = GetSoundSource(stream))
{
//SoundOut implementation which plays the sound
using (ISoundOut soundOut = GetSoundOut())
{
//Tell the SoundOut which sound it has to play
soundOut.Initialize(soundSource);
//Play the sound
soundOut.Play();
Thread.Sleep(2000);
//Stop the playback
soundOut.Stop();
}
}
}
private ISoundOut GetSoundOut()
{
if (WasapiOut.IsSupportedOnCurrentPlatform)
return new WasapiOut();
else
return new DirectSoundOut();
}
private IWaveSource GetSoundSource(Stream stream)
{
// Instead of using the CodecFactory as helper, you specify the decoder directly:
return new DmoMp3Decoder(stream);
}对于WMA,您可以使用WmaDecoder。您应该检查不同解码器的实现:https://github.com/filoe/cscore/blob/master/CSCore/Codecs/CodecFactory.cs#L30
确保不引发异常,并使用另一个解码器(Mp3MediafoundationDecoder)处理它们,如链接源代码中的那样。最后别忘了处理你的溪流。
https://stackoverflow.com/questions/37333969
复制相似问题