基本信息
源码名称:C# 录制音频并播放wav声音文件 例子源码
源码大小:1.95M
文件格式:.rar
开发语言:C#
更新时间:2015-08-24
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
public partial class Form1 : Form
{
private IAudioPlayer audioPlayer;
private IMicrophoneCapturer microphoneCapturer;
public Form1()
{
InitializeComponent();
}
private void button_mic_Click(object sender, EventArgs e)
{
try
{
this.microphoneCapturer = CapturerFactory.CreateMicrophoneCapturer(int.Parse(this.textBox_mic.Text));
this.microphoneCapturer.AudioCaptured = new ESBasic.CbGeneric<byte[]>(microphoneCapturer_AudioCaptured);
this.audioPlayer = PlayerFactory.CreateAudioPlayer(int.Parse(this.textBox_speaker.Text), 16000, 1, 16, 2);
this.microphoneCapturer.Start();
this.label_msg.Text = "正在采集麦克风,并播放 . . .";
this.label_msg.Visible = true;
this.button_wav.Enabled = false;
this.button_mic.Enabled = false;
this.button_stop.Enabled = true;
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
void microphoneCapturer_AudioCaptured(byte[] audioData)
{
if (this.audioPlayer != null)
{
this.audioPlayer.Play(audioData);
}
}
private void button_wav_Click(object sender, EventArgs e)
{
try
{
string path = ESBasic.Helpers.FileHelper.GetFileToOpen2("请选择要播放的wav文件", AppDomain.CurrentDomain.BaseDirectory, ".wav");
if (path == null)
{
return;
}
AudioInformation info = PlayerFactory.ParseWaveFile(path);
if (info.FormatTag != (int)WaveFormats.Pcm)
{
MessageBox.Show("仅仅支持PCM编码方式的语音数据!");
return;
}
int secs = info.GetTimeInMsecs() / 1000; //声音数据的播放时长
this.audioPlayer = PlayerFactory.CreateAudioPlayer(int.Parse(this.textBox_speaker.Text), info.SampleRate, info.ChannelCount, info.BitsNumber, secs 1);
this.audioPlayer.Play(info.AudioData);
this.label_msg.Text = "正在播放wav文件 . . .";
this.label_msg.Visible = true;
this.button_wav.Enabled = false;
this.button_mic.Enabled = false;
this.button_stop.Enabled = true;
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.microphoneCapturer != null)
{
this.microphoneCapturer.Stop();
this.microphoneCapturer.Dispose();
this.microphoneCapturer = null;
}
if (this.audioPlayer != null)
{
this.audioPlayer.Dispose();
this.audioPlayer = null;
}
}
private void button_stop_Click(object sender, EventArgs e)
{
if (this.audioPlayer == null)
{
return;
}
if (this.microphoneCapturer != null)
{
this.microphoneCapturer.Stop();
this.microphoneCapturer.Dispose();
this.microphoneCapturer = null;
}
this.audioPlayer.Clear();
this.audioPlayer.Dispose();
this.audioPlayer = null;
this.label_msg.Visible = false;
this.button_wav.Enabled = true;
this.button_mic.Enabled = true;
this.button_stop.Enabled = false;
}
}