c#获取麦克风数据并播放

来源:互联网 发布:图片清晰化软件 编辑:程序博客网 时间:2024/05/06 19:23

本程序使用了第三方(傲瑞科技)开发库:

  • ESBasic.dll
  • ESBasic.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;            }        }    }}

如果音频播放没有声音,请查看麦克风与扬声器的音量是否为零或禁用了,查看方式为进入控制面板-声音-扬声器(如果没有接设备可能没有)-属性-级别

0 0
原创粉丝点击