如何使用NAudio

时间:2018-06-04 19:13:18

标签: c# naudio

我试图通过hls与C#中的NAudio播放一个简单的加密mp3文件。

在MP3StreamingPanel.cs中有一个名为 StreamMp3 的函数,它使用 HttpWebRequest HttpWebResponse

private void StreamMp3(object state)
{
    fullyDownloaded = false;
    var url = (string)state;
    webRequest = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse resp;

    try
    {
        resp = (HttpWebResponse)webRequest.GetResponse();
    }
    catch(WebException e)
    {
        if (e.Status != WebExceptionStatus.RequestCanceled)
        {
            ShowError(e.Message);
        }
            return;
        }
        var buffer = new byte[16384 * 4]; 
        IMp3FrameDecompressor decompressor = null;

        try
        {
            using (var responseStream = resp.GetResponseStream())
            {
                var readFullyStream = new ReadFullyStream(responseStream);
                do
                {
                    if (IsBufferNearlyFull)
                    {
                        Debug.WriteLine("Buffer getting full, taking a break");
                        Thread.Sleep(500);
                    }
                    else
                    {
                        Mp3Frame frame;
                        try
                        {
                            frame = Mp3Frame.LoadFromStream(readFullyStream);
                        }
                        catch (EndOfStreamException)
                        {
                            fullyDownloaded = true;
                            break;
                        }
                        catch (WebException)
                        {
                            break;
                        }
                        if (decompressor == null)
                        {
                            decompressor = CreateFrameDecompressor(frame);
                            bufferedWaveProvider = new BufferedWaveProvider(decompressor.OutputFormat);
                            bufferedWaveProvider.BufferDuration = TimeSpan.FromSeconds(20); // allow us to get well ahead of ourselves
                        }
                        int decompressed = decompressor.DecompressFrame(frame, buffer, 0);
                        bufferedWaveProvider.AddSamples(buffer, 0, decompressed);
                    }

                } while (playbackState != StreamingPlaybackState.Stopped);
                Debug.WriteLine("Exiting");
                decompressor.Dispose();
            }
        }
        finally
        {
            if (decompressor != null)
            {
                decompressor.Dispose();
            }
        }
    }

我使用此代码通过XOR

解密字节
 byte[] Key = ASCIIEncoding.ASCII.GetBytes("Y");

 for (int i = 0; i < frame.RawData.Length; i++)
 {
     frame.RawData[i] = (byte)(Key[0] ^ frame.RawData[i]);
 }

问题是帧解密但播放速度非常快,最后我在for循环中出错:

附加信息:未将对象引用设置为对象的实例。

0 个答案:

没有答案