C# Naaudio BufferedWaveProvider 缓冲区满异常
Posted
技术标签:
【中文标题】C# Naaudio BufferedWaveProvider 缓冲区满异常【英文标题】:C# Naudio BufferedWaveProvider Buffer Full Exception 【发布时间】:2016-06-23 16:54:34 【问题描述】:当我使用 websocket 连接从 android 发送实时音频流时,我在 BufferedWaveProvider 上遇到缓冲区满异常。 从 Android 发送的音频格式:44100,Sterio,16bit 我也尝试过增加 BufferedWaveProvider 的缓冲区长度,但仍然不行,唯一改变的是程序运行时间更长,我还尝试使用 WaveFileWriter 将音频录制到文件中,该文件在 10 秒音频流中约为 80mb并且里面没有音频。
class Program
static WaveOut waveOut;
static BufferedWaveProvider bufferedWaveProvider = null;
private static SpeechSynthesizer ss;
private static SpeechRecognitionEngine sre;
private static int temperature = 75;
private static WebSocketServer appServer = new WebSocketServer();
private static int total = 0;
private static WaveFileWriter writer = new WaveFileWriter("recording.wav", new WaveFormat(44100, 16, 2));
private static MemoryStream memStream = new MemoryStream();
static void Main(string[] args)
Console.WriteLine("Press any key to start the WebSocketServer!");
Console.ReadKey();
Console.WriteLine();
waveOut = new WaveOut();
bufferedWaveProvider = new BufferedWaveProvider(new WaveFormat());
waveOut.Init(bufferedWaveProvider);
waveOut.Play();
appServer.NewDataReceived += new SessionHandler<WebSocketSession, byte[]>(appServer_NewDataReceived);
appServer.NewSessionConnected += AppServer_NewSessionConnected;
appServer.SessionClosed += new SessionHandler<WebSocketSession, CloseReason>(appServer_SessionClosed);
//Setup the appServer
if (!appServer.Setup(80)) //Setup with listening port
Console.WriteLine("Failed to setup!");
Console.ReadKey();
return;
//Try to start the appServer
if (!appServer.Start())
Console.WriteLine("Failed to start!");
Console.ReadKey();
return;
Console.WriteLine("The server started successfully, press key 'q' to stop it!");
while (Console.ReadKey().KeyChar != 'q')
Console.WriteLine();
continue;
//Stop the appServer
appServer.Stop();
Console.WriteLine();
Console.WriteLine("The server was stopped!");
Console.ReadKey();
static void appServer_NewDataReceived(WebSocketSession session, byte[] value)
total = total + value.Length;
Console.WriteLine("Receiving!! " + total);
bufferedWaveProvider.AddSamples(value, 0, value.Length);
【问题讨论】:
【参考方案1】:您需要从了解传入数据的格式开始。如果您录制的 wav 文件中没有音频,则说明有问题,尽管您必须在尝试访问之前调用 wave 文件编写器上的 Dispose。
【讨论】:
感谢 Mark Heath,第二天我发现我的 android 设备实际上并没有录制音频,所以流是空的,现在它可以完美运行,没有任何问题,抱歉回复晚了。以上是关于C# Naaudio BufferedWaveProvider 缓冲区满异常的主要内容,如果未能解决你的问题,请参考以下文章