WriteableBufferingSource使用固定的缓冲区大小。如果缓冲区已满,则无法添加新数据,解决了增加缓冲区大小的问题。
我正在播放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);
}
}
首先,对不起我的英语,我也这么做了,我检查播放器是Stop,你需要其中一个修复:
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=真实
CSCore.WaveFormat waveFormat = new CSCore.WaveFormat(Session.format.sample_rate, 16, Session.format.channels, CSCore.AudioEncoding.Pcm);
source = new WriteableBufferingSource(waveFormat, waveFormat.BytesPerSecond * 240);