NAudio Universal:等待活动超时

时间:2016-10-01 18:08:12

标签: c# wpf .net-4.0 windows-10-universal naudio

我无法使用Naudio为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.
}

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我是对的,你在这里得到了结果的长度:

var decodedLen = opusDecoder.Decode

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

short[] finalOutput = new short[frameSize];

这一行不应该是这样的:

short[] finalOutput = new short[decodedLen];