Mobile5、6下录音(OpenNETCF)
OpenNETCF提供了方便、强大的基于设备开发库,利用它,可以方便的进行录音实现。
三个功能:启动录音、停止录音、播放录音
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//using OpenNETCF.Media;
using System.IO;
using OpenNETCF.Media.WaveAudio;
namespace RecorderTest
{
public partial class MainForm : Form
{
private Stream recStream;
private Recorder recorder = new Recorder();
private Player player = new Player();
int counter = 0;
string filename = "test.wav";
Status status = Status.None;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
}
private void btnStart_Click(object sender, EventArgs e)
{
//开始录音
status = Status.Recordting; //设置状态为正在录音中
counter = 0;
this.timer1.Enabled = true;
RecordStart(GetPath(filename));
}
private void btnStop_Click(object sender, EventArgs e)
{
//停止录音
status = Status.None; //设置当前状态为无
RecordStop();
this.timer1.Enabled = false;
}
private void btnPlay_Click(object sender, EventArgs e)
{
//播放录音
status = Status.Playing; //设置当前状态为正在播放
Play(GetPath(filename));
}
/// <summary>
/// 开始录音
/// </summary>
/// <param name="recordFile"></param>
public void RecordStart(String recordFile)
{
try
{
//
if (File.Exists(recordFile))
{
File.Delete(recordFile);
}
recStream = File.OpenWrite(recordFile);
recorder.RecordFor(recStream, 60 * 10, SoundFormats.Mono8bit11kHz);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// 停止录音
/// </summary>
public void RecordStop()
{
while (recorder.Recording)
{
recorder.Stop();
System.Windows.Forms.Application.DoEvents();
}
}
/// <summary>
/// 播放录音
/// </summary>
/// <param name="recordFile"></param>
public void Play(String recordFile)
{
this.timer1.Enabled = true;
counter = 0;
Stream stream = File.OpenRead(recordFile);
player.Play(stream);
}
/// <summary>
/// 获取当前程序路径
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
private static string GetPath(string filename)
{
string CurrentPath = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
CurrentPath = Path.GetDirectoryName(CurrentPath);
return Path.Combine(CurrentPath, filename);
}
/// <summary>
/// 时间定时事件
/// </summary>
private void timer1_Tick(object sender, EventArgs e)
{
if (status==Status.Recordting) //当前状态为:正在录音
{
this.counter++;
this.labTime.Text = this.IntToTimeFormat(this.counter);
}
else if (status == Status.Playing) //当前状态为:正在播放
{
if (player.Playing)
{
this.counter++;
this.labTime.Text = this.IntToTimeFormat(this.counter);
}
else
{
this.timer1.Enabled = false;
}
}
else
{ this.timer1.Enabled = false; }
}
/// <summary>
/// 格式化时间 mm:ss
/// </summary>
/// <param name="argInt"></param>
/// <returns></returns>
private string IntToTimeFormat(int argInt)
{
string str = "";
int num = 0;
str = str + ((int)Math.Floor((double)(argInt / 60))).ToString() + ":";
num = argInt % 60;
return (str + num.ToString("00"));
}
}
/// <summary>
/// 当前状态
/// </summary>
enum Status
{
/// <summary>
/// 无状态
/// </summary>
None,
/// <summary>
/// 正在录音
/// </summary>
Recordting,
/// <summary>
/// 正在播放
/// </summary>
Playing
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//using OpenNETCF.Media;
using System.IO;
using OpenNETCF.Media.WaveAudio;
namespace RecorderTest
{
public partial class MainForm : Form
{
private Stream recStream;
private Recorder recorder = new Recorder();
private Player player = new Player();
int counter = 0;
string filename = "test.wav";
Status status = Status.None;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
}
private void btnStart_Click(object sender, EventArgs e)
{
//开始录音
status = Status.Recordting; //设置状态为正在录音中
counter = 0;
this.timer1.Enabled = true;
RecordStart(GetPath(filename));
}
private void btnStop_Click(object sender, EventArgs e)
{
//停止录音
status = Status.None; //设置当前状态为无
RecordStop();
this.timer1.Enabled = false;
}
private void btnPlay_Click(object sender, EventArgs e)
{
//播放录音
status = Status.Playing; //设置当前状态为正在播放
Play(GetPath(filename));
}
/// <summary>
/// 开始录音
/// </summary>
/// <param name="recordFile"></param>
public void RecordStart(String recordFile)
{
try
{
//
if (File.Exists(recordFile))
{
File.Delete(recordFile);
}
recStream = File.OpenWrite(recordFile);
recorder.RecordFor(recStream, 60 * 10, SoundFormats.Mono8bit11kHz);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// 停止录音
/// </summary>
public void RecordStop()
{
while (recorder.Recording)
{
recorder.Stop();
System.Windows.Forms.Application.DoEvents();
}
}
/// <summary>
/// 播放录音
/// </summary>
/// <param name="recordFile"></param>
public void Play(String recordFile)
{
this.timer1.Enabled = true;
counter = 0;
Stream stream = File.OpenRead(recordFile);
player.Play(stream);
}
/// <summary>
/// 获取当前程序路径
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
private static string GetPath(string filename)
{
string CurrentPath = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
CurrentPath = Path.GetDirectoryName(CurrentPath);
return Path.Combine(CurrentPath, filename);
}
/// <summary>
/// 时间定时事件
/// </summary>
private void timer1_Tick(object sender, EventArgs e)
{
if (status==Status.Recordting) //当前状态为:正在录音
{
this.counter++;
this.labTime.Text = this.IntToTimeFormat(this.counter);
}
else if (status == Status.Playing) //当前状态为:正在播放
{
if (player.Playing)
{
this.counter++;
this.labTime.Text = this.IntToTimeFormat(this.counter);
}
else
{
this.timer1.Enabled = false;
}
}
else
{ this.timer1.Enabled = false; }
}
/// <summary>
/// 格式化时间 mm:ss
/// </summary>
/// <param name="argInt"></param>
/// <returns></returns>
private string IntToTimeFormat(int argInt)
{
string str = "";
int num = 0;
str = str + ((int)Math.Floor((double)(argInt / 60))).ToString() + ":";
num = argInt % 60;
return (str + num.ToString("00"));
}
}
/// <summary>
/// 当前状态
/// </summary>
enum Status
{
/// <summary>
/// 无状态
/// </summary>
None,
/// <summary>
/// 正在录音
/// </summary>
Recordting,
/// <summary>
/// 正在播放
/// </summary>
Playing
}
}
本博客文章版权归博客园和solan3000共同所有。