c#获取麦克风数据并播放
2016-10-22 09:28
155 查看
本程序使用了第三方(傲瑞科技)开发库:
ESBasic.dllESBasic.xml
Oraycn.MCapture.dll
Oraycn.MCapture.xml
Oraycn.MPlayer.dll
Oraycn.MPlayer.xml
访问官方网址
将以上文件拷贝到项目Debug目录下,并在项目中添加引用
捕获音频数据类
using Oraycn.MCapture; using RemoteDiagnosis.net; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RemoteDiagnosis.audio { public class MicrophoneHelper { private static MicrophoneHelper micphoneHelper = new MicrophoneHelper(); private IMicrophoneCapturer microphoneCapturer; public bool started = false; private MicrophoneHelper() { } public static MicrophoneHelper getIns() { return micphoneHelper; } public bool start() { try { this.microphoneCapturer = CapturerFactory.CreateMicrophoneCapturer(0); this.microphoneCapturer.AudioCaptured += new ESBasic.CbGeneric<byte[]>(microphoneCapturer_AudioCaptured); //this.audioPlayer = PlayerFactory.CreateAudioPlayer(0, 16000, 1, 16, 3); this.microphoneCapturer.Start(); started = true; } catch (Exception ) { started = false; } return started; } //捕获麦克风的音频数据 void microphoneCapturer_AudioCaptured(byte[] audioData) { MyAudioClient.getIns().send(audioData); //SpeakerHelper.getIns().speak(audioData); } public void stop() { if (this.microphoneCapturer != null) { this.microphoneCapturer.Stop(); this.microphoneCapturer.Dispose(); this.microphoneCapturer = null; } } } }
播放捕获的音频数据
using Oraycn.MPlayer; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RemoteDiagnosis.audio { public class SpeakerHelper { private static SpeakerHelper speaker = new SpeakerHelper(); private IAudioPlayer audioPlayer; private SpeakerHelper() { this.audioPlayer = PlayerFactory.CreateAudioPlayer(0, 16000, 1, 16, 3); //this.audioPlayer = PlayerFactory.CreateAudioPlayer(0, 16000, 2, 16, 3); } public static SpeakerHelper getIns() { return speaker; } /// <summary> /// 播放数据 /// </summary> /// <param name="buffer"></param> public void speak(byte[] buffer) { if (this.audioPlayer != null) { //Debug.WriteLine("audioData :" + audioData.Length); this.audioPlayer.Play(buffer); } } public void stop() { if (this.audioPlayer != null) { this.audioPlayer.Clear(); this.audioPlayer.Dispose(); this.audioPlayer = null; } } } }
如果音频播放没有声音,请查看麦克风与扬声器的音量是否为零或禁用了,查看方式为进入控制面板-声音-扬声器(如果没有接设备可能没有)-属性-级别
相关文章推荐
- C# 获取页面Post过来的数据
- C#操作数据库获取指定列数据类型为int的值
- C# 如何获取SQL Server 中指定数据表的所有字段名和字段类型
- C# 获取DataTable数据导出到Excel
- C# 语言如何获取json格式的数据,不用javascript用c#实现。。。
- C# Socket实现通过公共服务程序获取数据的例子
- C#获取MySql 数据常用的代码
- C# .NET 后台 json 转 类库 获取数据
- C#之获取ListView当前选中的行的数据!...
- 使用C#获取Windows Phone手机的各种数据(转)
- 用效劳来获取网络图片和播放音乐的比如。主要是效劳传递数据的操练
- C#获取Excel数据到Datatabel(以流的方式)
- 在C#中使用WIA获取扫描仪数据
- C#获取oracle数据库某表的列名和数据类型【转载】
- C#Winform从页面获取数据,传入数据库
- C# 获取ListView中选中行中对应的列数据
- 在C#中使用WIA获取扫描仪数据
- C# 中GPS数据的获取的基类
- 【C#获取音频文件的播放时间】
- 获取串口数据 (C#)