首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CSCore -播放来自FileStream或MemoryStream的音频

CSCore -播放来自FileStream或MemoryStream的音频
EN

Stack Overflow用户
提问于 2016-05-19 21:04:39
回答 1查看 4K关注 0票数 5

使用CSCore,如何从FileStreamMemoryStream中播放WMA或MP3 (与使用以string作为文件路径或url的方法不同)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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上文档中的调整样本

代码语言:javascript
复制
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)处理它们,如链接源代码中的那样。最后别忘了处理你的溪流。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37333969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档