NAudio Universal:等待事件超时

本文关键字:事件 超时 等待 Universal NAudio | 更新日期: 2023-09-27 18:09:45

我无法使用Naudio for Windows Universal播放解码的opus数据包。声音播放一秒钟,然后异常停止。我收到的异常是InvalidOperation - Timed out waiting for event

下面是我的代码:
public void ForStackOverFlow()
{
    var waveOut = new WasapiOutRT(AudioClientShareMode.Shared, 200);
    waveOut.PlaybackStopped += WaveOut_PlaybackStopped;
    var opusDecoder = OpusDecoder.Create(48000, 1);
    var ms = new MemoryStream();
    var voiceData1 = "3OxQRhb / datLhcmWgOhYPrYGwxyciCulHDwJl8RmJHkdWVcENoAYvgLtcFn0LBLbLJ2jHRPv8Wb / oZSsTp3oJbllORxffNIyZO1TCCf2L + dkN5AMKRmiIyYaJQrh5Bt4ucLfNskhwKwz / CfLV7RRTZrBJM + 0AqBXS9dt3 + yeBofItCGUm / GCT94pW5fl9U7Zez + RodOAzyEemdCS + Zfpquw0XctnM0L / b1llt3oHZOpot1BUY + tDyXmSNbcfxPjYmVvPT2y4aQ ==";
    var sample = Convert.FromBase64String(voiceData1);
    int frameSize = 1920;  
    short[] outputBuffer = new short[frameSize];
    var decodedLen = opusDecoder.Decode(sample, 0, sample.Length, outputBuffer, 0, frameSize, false);
    short[] finalOutput = new short[frameSize];
    Array.Copy(outputBuffer, finalOutput, finalOutput.Length); 
    var audioBytes = AudioMath.ShortsToBytes(finalOutput, 0, finalOutput.Length); 
    System.Diagnostics.Debug.WriteLine("AUDIOBYTES:" + Convert.ToBase64String(audioBytes));
    ms.Write(audioBytes, 0, audioBytes.Length);
    ms.Flush();
    ms.Seek(0, SeekOrigin.Begin);
    IWaveProvider provider = new RawSourceWaveStream(ms, new WaveFormat(48000, 16, 1));
    waveOut.Init(provider);
    waveOut.Play();
}
private void WaveOut_PlaybackStopped(object sender, StoppedEventArgs e)
{
    //event fires with exception.
}

NAudio Universal:等待事件超时

你得到的结果长度对吗?

var decodedLen = opusDecoder.Decode

如果是,为什么不使用该变量创建finalOutput数组,并使用framesize常数代替?

short[] finalOutput = new short[frameSize];

这行不应该是这样的吗?

short[] finalOutput = new short[decodedLen];