使用NAudio通过网络传输语音 [英] USing NAudio to transfer voice over the network

查看:112
本文介绍了使用NAudio通过网络传输语音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我正在使用NAudio库通过网络传输语音,即从我的麦克风传输到任何其他远程PC扬声器,我需要一些帮助我已经使用Naudio库从麦克风录制语音并存储它,但现在当我尝试通过网络发送它时,我遇到了问题。 我正在使用以下代码(发件人方面),但它给我一个例外,Wavein应​​该在后台线程中记录为什么会这样???





 使用系统; 
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading;

命名空间 ConsoleApplication2
{
class Program
{
private NAudio.Wave.WaveIn sourcestream = null ;
private NAudio.Wave.DirectSoundOut waveOut = null ;
private NAudio.Wave.WaveFileWriter Wavewriter = null ;

静态 void Main( string [] args)
{
Program obj = new Program();
obj.function();

}
void function()
{
Thread t = new Thread( new ThreadStart(Service));
t.IsBackground = true ;
t.Start();
// 线程t2 =新线程(新的ThreadStart(Service2));
// t2.Start();


}

void 服务()
{
sourcestream = new NAudio.Wave.WaveIn();

while true
{

int devicenumber = 0 ;

sourcestream.DeviceNumber = devicenumber;

sourcestream.WaveFormat = new NAudio.Wave.WaveFormat( 44100 ,NAudio .Wave.WaveIn.GetCapabilities(devicenumber).Channels);

sourcestream.DataAvailable + = new EventHandler< naudio.wave.waveineventargs>(sourcestream_DataAvailable);
// Wavewriter = new NAudio.Wave.WaveFileWriter(save.FileName,sourcestream.WaveFormat);


}
}
// void Service2 {
// }

private void sourcestream_DataAvailable( object sender,NAudio .Wave.WaveInEventArgs e)
{
sourcestream.StartRecording();
byte [] buffer = e.Buffer;
Console.WriteLine(buffer);

}
}
}

解决方案



 sourcestream 





将init块放入主函数

 sourcestream = new NAudio.Wave.WaveIn(); 



等设置



删除while循环



把它放到线程的

服务

函数

 sourcestream.StartRecording(); 


Hi guys

I am using the NAudio library to transfer voice over the network i.e. from my microphone to any other remote PC speakers for that i need some help i have used the Naudio library to record a voice from microphone and store it but now when i am trying to send it over the network i am having problems. I am using the following Code(Sender side) but it give me an Exception that Wavein should record in background thread why is this so ??


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication2
{
    class Program
    {
        private NAudio.Wave.WaveIn sourcestream = null;
        private NAudio.Wave.DirectSoundOut waveOut = null;
        private NAudio.Wave.WaveFileWriter Wavewriter = null;
        
        static void Main(string[] args)
        {
            Program obj = new Program();
            obj.function();
            
        }
        void function()
        {
            Thread t = new Thread(new ThreadStart(Service));
            t.IsBackground = true;
            t.Start();
            //Thread t2 = new Thread(new ThreadStart(Service2));
            //t2.Start();


        }

            void Service()
            {
                sourcestream = new NAudio.Wave.WaveIn();
                
                while(true)
                {
            
            int devicenumber = 0;
            
            sourcestream.DeviceNumber = devicenumber;

            sourcestream.WaveFormat = new NAudio.Wave.WaveFormat(44100, NAudio.Wave.WaveIn.GetCapabilities(devicenumber).Channels);
                  
            sourcestream.DataAvailable += new EventHandler<naudio.wave.waveineventargs>(sourcestream_DataAvailable);
            //Wavewriter = new NAudio.Wave.WaveFileWriter(save.FileName,sourcestream.WaveFormat);

            
            }
            }
            //void Service2 { 
            //}

            private void sourcestream_DataAvailable(object sender, NAudio.Wave.WaveInEventArgs e )
            {
                sourcestream.StartRecording();
                byte[] buffer = e.Buffer;
                Console.WriteLine(buffer);               

            }
        }
    }

解决方案

add private variable for

sourcestream 



put init block to Main function

sourcestream = new NAudio.Wave.WaveIn();


and other setting

delete while loop

put this to thread's

Service

function

sourcestream.StartRecording();


这篇关于使用NAudio通过网络传输语音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆