【问题标题】:Playing WriteableBufferingSource speeds up after 5 sec播放 WriteableBufferingSource 5 秒后加速
【发布时间】:2015-02-18 18:01:50
【问题描述】:

我正在播放 PCM RAW 格式的音乐,前 5 秒播放正确,但之后播放速度提高了一倍

这是我所做的: 我有一个带有事件处理程序的播放器类,当程序从某个位置接收到一个字节时,它会添加到一个队列中,并从这个队列中添加一个线程到缓冲区。

我得到的音乐来自 spotify,使用 libspotifydotnet,CSCore.Codecs.RAW.RawDataReader 播放正确,但我不能在播放时继续向流中添加更多数据,或者我可以吗???!!

这是我到目前为止所做的事情

    //Main.cs
    WasapiOut soundOut = new WasapiOut();
    soundOut.Initialize(Player.source);
    soundOut.Play();

...

   //Player.cs
    public static WriteableBufferingSource source;
    private static Queue<byte[]> _q = new Queue<byte[]>();

    source = new WriteableBufferingSource(new CSCore.WaveFormat(Session.format.sample_rate, 16, Session.format.channels, CSCore.AudioEncoding.Pcm));
    source.FillWithZeros = false;
    byte[] buffer = null;
    while (!_interrupt && !_complete)
    {
        if (_q.Count > 0)
        {
            buffer = _q.Dequeue();

            source.Write(buffer, 0, buffer.Length);
            System.Console.WriteLine("Buffer written {0} bytes", buffer.Length);
            Thread.Sleep(10);
        }
    }

    //New data downloaded event
    private static void Session_OnAudioDataArrived(byte[] buffer)
    {
        if (!_interrupt && !_complete)
        {
            _q.Enqueue(buffer);
        }
    }

【问题讨论】:

    标签: c# libspotify cscore


    【解决方案1】:

    首先,对不起我的英语,我也是这样做的,我检查玩家是否停止,你需要这个修复之一:

         while (!_interrupt && !_complete)
            {
                if (_q.Count > 0)
                {
                    buffer = _q.Dequeue();
                    source.Write(buffer, 0, buffer.Length);
    
    // Check is playing
        if (soundOut.PlaybackState == PlaybackState.Stopped)
                        soundOut.Play();
    
                    System.Console.WriteLine("Buffer written {0} bytes", buffer.Length);
                    Thread.Sleep(10);
                }
            }
    

    或者放

    FillWithZeros=true

    【讨论】:

      【解决方案2】:

      WriteableBufferingSource 使用固定的缓冲区大小。如果缓冲区已满,则无法添加新数据,解决增加缓冲区大小。

      CSCore.WaveFormat waveFormat = new CSCore.WaveFormat(Session.format.sample_rate, 16, Session.format.channels, CSCore.AudioEncoding.Pcm);
      
      source = new WriteableBufferingSource(waveFormat, waveFormat.BytesPerSecond * 240);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-30
        • 1970-01-01
        • 1970-01-01
        • 2013-12-23
        • 2012-09-15
        • 1970-01-01
        相关资源
        最近更新 更多