简体   繁体   English

处理NAudio中mp3内存流的WaveFormat设置

[英]Handle setting up WaveFormat for mp3 memory stream in NAudio

I'm trying to set up the WaveStream so that it uses the same format as the mp3 data passed in. I get the format by reading a frame, but when I try to actually create the new conversion stream using the new format I get an "AcmNotPossible calling AcmStreamOpen" exception. 我正在尝试设置WaveStream,使其使用与传入的mp3数据相同的格式。我通过读取帧来获取格式,但是当我尝试使用新格式实际创建新的转换流时, “ AcmNotPossible调用AcmStreamOpen”异常。

Here's where I'm trying to set the new format: 这是我尝试设置新格式的地方:

Mp3Frame f = Mp3Frame.LoadFromStream(ms);
WaveFormat targetFormat = new Mp3WaveFormat(f.SampleRate, f.ChannelMode == ChannelMode.Mono ? 1 : 2, f.FrameLength, f.BitRate);
WaveFormatConversionStream conversionStream;
try
{
    using (WaveStream blockAlignedStream =
        new BlockAlignReductionStream(conversionStream = new WaveFormatConversionStream(targetFormat,
                new Mp3FileReader(ms))))
    {
        using (WaveOut waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
        {
            waveOut.Init(blockAlignedStream);
            waveOut.Play();

I'm not sure if I even need to convert anything if I set up the wave stream to match the format of the mp3 data. 我不确定是否将波形流设置为与mp​​3数据的格式匹配,是否还需要转换任何内容。

NOTE: I tried using WaveFormatStream.CreatePcmStream but I was getting 'static/white noise' for some mp3's. 注意:我尝试使用WaveFormatStream.CreatePcmStream,但某些mp3却出现“静态/白噪声”。 They appeared to be 16bit 44,100 stereo, but were being labeled as Version 1, Layer 1, as opposed to version 1, layer 3 which plays back correctly. 它们似乎是16位44,100立体声,但被标记为版本1,第1层,与版本1,第3层正确播放相反。

This code sample seems to have come from a long time back. 此代码示例似乎来自很久以前。 You don;t need the BlockAlignReductionStream or the WaveFormatConversionStream , and you should stay away from function callbacks in WaveOut . 您不需要BlockAlignReductionStreamWaveFormatConversionStream ,并且应远离WaveOut函数回调。 This should be sufficient to play from a memory stream: 这足以从内存流中播放:

var reader = new Mp3FileReader(ms)
var waveOut = new WaveOutEvent();
waveOut.Init(reader);
waveOut.Play();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM