简体   繁体   English

C#Naudio BufferedWaveProvider缓冲区完全异常

[英]C# Naudio BufferedWaveProvider Buffer Full Exception

I am getting a buffer full exception on BufferedWaveProvider when I am sending a real time audio stream from Android using websocket connection. 我正在使用websocket连接从Android发送实时音频流时,在BufferedWaveProvider上收到缓冲区已满异常。 Audio format Sent from Android: 44100, Sterio, 16bit I have also tried increasing the buffer length of BufferedWaveProvider but still no good, the only thing changed is that the program runs a little longer, Also I tried recording the audio to a file using WaveFileWriter, the file was around 80mb for 10 seconds audio stream and has no audio in it. 从Android发送的音频格式:44100,Sterio,16位我也尝试增加BufferedWaveProvider的缓冲区长度,但仍然没有效果,唯一的变化是程序运行了更长的时间,而且我尝试使用WaveFileWriter将音频记录到文件中,该文件约为80mb,持续10秒的音频流,并且其中没有音频。

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);
    }

well you need to start by understanding what the format of the incoming data is. 好,您需要先了解传入数据的格式。 If the wav file you recorded has no audio in it, then something is wrong, although you must call Dispose on the wave file writer before attempting to access it. 如果您录制的wav文件中没有音频,则出了点问题,尽管您在尝试访问它之前必须在wave文件编写器上调用Dispose。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM