1、先上漂亮的界面图
2、引用OpennetCF 2.2.0.0组件,代码如下
Code
初始化事件#region 初始化事件
private Player player;
private Recorder recorder;
private Stream stream;
//录音最大时间(秒)
private const int RECORD_LENGTH = 3600;
private string TEMP_PATH = Bussiness.Global.AccessoryPath;
private string strfileName;
private string strCamreaName;
private int pauseAndRestart = 0;
private int ChangeFile = 0;
private float filelength = 0;
private string filter = "*.*"; //文件类型
private int m_numFiles = 0; //文件总数
ArrayList m_pathList = new ArrayList();//包含所有文件路径的数组
private string[] files; //所有文件名
public NewMail newmail { get; set; }
public string FType { get; set; }
public string FRecordPath { get; set; }
public FileManage(string p_strType, NewMail p_objectNewmail)
{
InitializeComponent();
FType = p_strType;
newmail = p_objectNewmail;
}
private void FileManage_Load(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
if (FType == "RECORD")
{
tabCFileManage.SelectedIndex = 1;
}
if (FType == "ACCESS")
{
tabCFileManage.SelectedIndex = 2;
}
if (FType == "YUYIN")
{
tabCFileManage.SelectedIndex = 1;
int index = FRecordPath.LastIndexOf(".");
int m_index = FRecordPath.LastIndexOf("\\");
if (FRecordPath.Substring(index + 1).ToUpper() == "WAV")
{
if (FRecordPath.Contains("\\"))
{
ChangeFile = 1;
pauseAndRestart = 0;
strfileName = FRecordPath.Substring(m_index + 1, index - m_index - 1);
}
}
else
{
MessageBox.Show("请选择WAV格式的文件!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
//实例化录音机
recorder = new Recorder();
recorder.DoneRecording += new WaveFinishedHandler(recorder_DoneRecording);
//实例化播放器
player = new Player();
player.DonePlaying += new WaveDoneHandler(player_DonePlaying);
labelViewTime.Text = "00:00";
labelState.Text = "无";
if (FType == "YUYIN")
labelCurrent.Text = strfileName;
else
labelCurrent.Text = " 暂无录音文件";
this.timer1.Enabled = false;
//附件初始化
ParseDirectory(TEMP_PATH, "*.*"); //初始化文件目录
files = CreatePathList(); //生成文件名数组
Cursor.Current = Cursors.Default;
}
private void menuItemBack_Click(object sender, EventArgs e)
{
this.Close();
}
#endregion
初始化事件#region 初始化事件
private Player player;
private Recorder recorder;
private Stream stream;
//录音最大时间(秒)
private const int RECORD_LENGTH = 3600;
private string TEMP_PATH = Bussiness.Global.AccessoryPath;
private string strfileName;
private string strCamreaName;
private int pauseAndRestart = 0;
private int ChangeFile = 0;
private float filelength = 0;
private string filter = "*.*"; //文件类型
private int m_numFiles = 0; //文件总数
ArrayList m_pathList = new ArrayList();//包含所有文件路径的数组
private string[] files; //所有文件名
public NewMail newmail { get; set; }
public string FType { get; set; }
public string FRecordPath { get; set; }
public FileManage(string p_strType, NewMail p_objectNewmail)
{
InitializeComponent();
FType = p_strType;
newmail = p_objectNewmail;
}
private void FileManage_Load(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
if (FType == "RECORD")
{
tabCFileManage.SelectedIndex = 1;
}
if (FType == "ACCESS")
{
tabCFileManage.SelectedIndex = 2;
}
if (FType == "YUYIN")
{
tabCFileManage.SelectedIndex = 1;
int index = FRecordPath.LastIndexOf(".");
int m_index = FRecordPath.LastIndexOf("\\");
if (FRecordPath.Substring(index + 1).ToUpper() == "WAV")
{
if (FRecordPath.Contains("\\"))
{
ChangeFile = 1;
pauseAndRestart = 0;
strfileName = FRecordPath.Substring(m_index + 1, index - m_index - 1);
}
}
else
{
MessageBox.Show("请选择WAV格式的文件!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
//实例化录音机
recorder = new Recorder();
recorder.DoneRecording += new WaveFinishedHandler(recorder_DoneRecording);
//实例化播放器
player = new Player();
player.DonePlaying += new WaveDoneHandler(player_DonePlaying);
labelViewTime.Text = "00:00";
labelState.Text = "无";
if (FType == "YUYIN")
labelCurrent.Text = strfileName;
else
labelCurrent.Text = " 暂无录音文件";
this.timer1.Enabled = false;
//附件初始化
ParseDirectory(TEMP_PATH, "*.*"); //初始化文件目录
files = CreatePathList(); //生成文件名数组
Cursor.Current = Cursors.Default;
}
private void menuItemBack_Click(object sender, EventArgs e)
{
this.Close();
}
#endregion
Code
#region 录音/播放
//录音中的事件
void recorder_DoneRecording()
{
}
//播放中的事件
void player_DonePlaying(object sender, IntPtr wParam, IntPtr lParam)
{
}
/// <summary>
/// 录音
/// </summary>
private void btnRecord_Click(object sender, EventArgs e)
{
try
{
if (player.Playing)
player.Stop();
labelState.Text = "录音中";
labelViewTime.Text = "00:00";
strfileName = Global.UserID + "-Record-" + String.Format("{0}月{1}时{2}分{3}秒", DateTime.Now.Month.ToString(), DateTime.Now.Hour.ToString(), DateTime.Now.Minute.ToString(), DateTime.Now.Second.ToString());
string FilePath = string.Format("{0}\\{1}.wav", Bussiness.Global.AccessoryPath, strfileName);
//检查临时录音文件是否存在
if (File.Exists(FilePath))
{
if (DialogResult.Yes == MessageBox.Show("存在相同文件名的录音文件是否覆盖?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
{
File.Delete(string.Format(FilePath));
Record(FilePath);
}
else
{
FilePath = string.Format("{0}\\{1}.wav", Bussiness.Global.AccessoryPath, strfileName + Guid.NewGuid().ToString());
Record(FilePath);
}
}
else
Record(FilePath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
private void Record(string FilePath)
{
try
{
stream = File.OpenWrite(FilePath);
labelCurrent.Text = strfileName;
labelViewTime.Text = "00:00";
timer1.Enabled = true;
this.timer1.Interval = 1000;
recorder.RecordFor(stream, RECORD_LENGTH, SoundFormats.Mono8bit11kHz);
btnRecord.Enabled = false;
btnPlay.Enabled = false;
btnOpenFile.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
/// <summary>
/// 停止
/// </summary>
private void btnStop_Click(object sender, EventArgs e)
{
try
{
if (recorder.Recording)
{
recorder.Stop();
timer1.Enabled = false;
btnRecord.Enabled = true;
btnPlay.Enabled = true;
btnOpenFile.Enabled = true;
this.trackBar1.Value = 0;
labelState.Text = "停止";
ParseDirectory(TEMP_PATH, "*.*");
}
pauseAndRestart = 0;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
/// <summary>
/// 播放
/// </summary>
private void btnPlay_Click(object sender, EventArgs e)
{
try
{
if (player.Playing || pauseAndRestart == 1 || ChangeFile == 2)
{
//暂停
if (pauseAndRestart == 1)
{
player.Restart();
timer2.Enabled = true;
labelState.Text = "播放";
pauseAndRestart = 0;
imageList2.ImageSize = new Size(52, 56);
btnPlay.Image = imageList2.Images[1];
btnRecord.Enabled = false;
btnCancel.Enabled = false;
btnStop.Enabled = false;
btnOpenFile.Enabled = false;
}
else
{
player.Pause();
timer2.Enabled = false;
pauseAndRestart = 1;
labelState.Text = "暂停";
imageList2.ImageSize = new Size(52, 56);
btnPlay.Image = imageList2.Images[0];
btnRecord.Enabled = true;
btnCancel.Enabled = true;
btnStop.Enabled = true;
btnOpenFile.Enabled = true;
}
}
else if (!string.IsNullOrEmpty(strfileName))
{
string AudioPath = Global.AccessoryPath + strfileName + ".wav";
Stream s = File.OpenRead(AudioPath);
player.Play(s);
FileInfo fileinfo = new FileInfo(AudioPath);
float m_value = fileinfo.Length / 1024;
filelength = m_value / 11;
labelViewTime.Text = "00:00";
labelState.Text = "播放";
imageList2.ImageSize = new Size(52, 56);
btnPlay.Image = imageList2.Images[1];
timer2.Enabled = true;
this.timer2.Interval = 1000;
btnRecord.Enabled = false;
btnCancel.Enabled = false;
btnStop.Enabled = false;
btnOpenFile.Enabled = false;
ChangeFile = 2;
}
else
{
MessageBox.Show("对不起,要播放的文件不存在!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
catch (Exception ex)
{
MessageBox.Show("找不到音频文件或者源文件有错!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
//取消
private void btnCancel_Click(object sender, EventArgs e)
{
try
{
Cursor.Current = Cursors.WaitCursor;
strfileName = Global.AccessoryPath + strfileName + ".wav";
if (recorder.Recording)
{
recorder.Stop();
timer1.Enabled = false;
btnRecord.Enabled = true;
btnPlay.Enabled = true;
btnOpenFile.Enabled = true;
this.trackBar1.Value = 0;
labelViewTime.Text = "00:00";
labelState.Text = "取消";
labelCurrent.Text = " 暂无录音文件";
ParseDirectory(TEMP_PATH, "*.*");
Cursor.Current = Cursors.Default;
}
Cursor.Current = Cursors.Default;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
/// <summary>
/// 遍历是否只选中一个
/// </summary>
private bool CheckLV(ref string m_message)
{
int i = 0;
foreach (ListViewItem lv in LVShowRecord.Items)
{
if (lv.Checked)
{
i++;
}
}
if (i < 1)
{
m_message = "请选择文件!";
}
if (i > 1)
{
m_message = "一次只能选择一个文件!";
}
return i == 1 ? true : false;
}
#endregion
#region 录音/播放
//录音中的事件
void recorder_DoneRecording()
{
}
//播放中的事件
void player_DonePlaying(object sender, IntPtr wParam, IntPtr lParam)
{
}
/// <summary>
/// 录音
/// </summary>
private void btnRecord_Click(object sender, EventArgs e)
{
try
{
if (player.Playing)
player.Stop();
labelState.Text = "录音中";
labelViewTime.Text = "00:00";
strfileName = Global.UserID + "-Record-" + String.Format("{0}月{1}时{2}分{3}秒", DateTime.Now.Month.ToString(), DateTime.Now.Hour.ToString(), DateTime.Now.Minute.ToString(), DateTime.Now.Second.ToString());
string FilePath = string.Format("{0}\\{1}.wav", Bussiness.Global.AccessoryPath, strfileName);
//检查临时录音文件是否存在
if (File.Exists(FilePath))
{
if (DialogResult.Yes == MessageBox.Show("存在相同文件名的录音文件是否覆盖?", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
{
File.Delete(string.Format(FilePath));
Record(FilePath);
}
else
{
FilePath = string.Format("{0}\\{1}.wav", Bussiness.Global.AccessoryPath, strfileName + Guid.NewGuid().ToString());
Record(FilePath);
}
}
else
Record(FilePath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
private void Record(string FilePath)
{
try
{
stream = File.OpenWrite(FilePath);
labelCurrent.Text = strfileName;
labelViewTime.Text = "00:00";
timer1.Enabled = true;
this.timer1.Interval = 1000;
recorder.RecordFor(stream, RECORD_LENGTH, SoundFormats.Mono8bit11kHz);
btnRecord.Enabled = false;
btnPlay.Enabled = false;
btnOpenFile.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
/// <summary>
/// 停止
/// </summary>
private void btnStop_Click(object sender, EventArgs e)
{
try
{
if (recorder.Recording)
{
recorder.Stop();
timer1.Enabled = false;
btnRecord.Enabled = true;
btnPlay.Enabled = true;
btnOpenFile.Enabled = true;
this.trackBar1.Value = 0;
labelState.Text = "停止";
ParseDirectory(TEMP_PATH, "*.*");
}
pauseAndRestart = 0;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
/// <summary>
/// 播放
/// </summary>
private void btnPlay_Click(object sender, EventArgs e)
{
try
{
if (player.Playing || pauseAndRestart == 1 || ChangeFile == 2)
{
//暂停
if (pauseAndRestart == 1)
{
player.Restart();
timer2.Enabled = true;
labelState.Text = "播放";
pauseAndRestart = 0;
imageList2.ImageSize = new Size(52, 56);
btnPlay.Image = imageList2.Images[1];
btnRecord.Enabled = false;
btnCancel.Enabled = false;
btnStop.Enabled = false;
btnOpenFile.Enabled = false;
}
else
{
player.Pause();
timer2.Enabled = false;
pauseAndRestart = 1;
labelState.Text = "暂停";
imageList2.ImageSize = new Size(52, 56);
btnPlay.Image = imageList2.Images[0];
btnRecord.Enabled = true;
btnCancel.Enabled = true;
btnStop.Enabled = true;
btnOpenFile.Enabled = true;
}
}
else if (!string.IsNullOrEmpty(strfileName))
{
string AudioPath = Global.AccessoryPath + strfileName + ".wav";
Stream s = File.OpenRead(AudioPath);
player.Play(s);
FileInfo fileinfo = new FileInfo(AudioPath);
float m_value = fileinfo.Length / 1024;
filelength = m_value / 11;
labelViewTime.Text = "00:00";
labelState.Text = "播放";
imageList2.ImageSize = new Size(52, 56);
btnPlay.Image = imageList2.Images[1];
timer2.Enabled = true;
this.timer2.Interval = 1000;
btnRecord.Enabled = false;
btnCancel.Enabled = false;
btnStop.Enabled = false;
btnOpenFile.Enabled = false;
ChangeFile = 2;
}
else
{
MessageBox.Show("对不起,要播放的文件不存在!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
catch (Exception ex)
{
MessageBox.Show("找不到音频文件或者源文件有错!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
//取消
private void btnCancel_Click(object sender, EventArgs e)
{
try
{
Cursor.Current = Cursors.WaitCursor;
strfileName = Global.AccessoryPath + strfileName + ".wav";
if (recorder.Recording)
{
recorder.Stop();
timer1.Enabled = false;
btnRecord.Enabled = true;
btnPlay.Enabled = true;
btnOpenFile.Enabled = true;
this.trackBar1.Value = 0;
labelViewTime.Text = "00:00";
labelState.Text = "取消";
labelCurrent.Text = " 暂无录音文件";
ParseDirectory(TEMP_PATH, "*.*");
Cursor.Current = Cursors.Default;
}
Cursor.Current = Cursors.Default;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
/// <summary>
/// 遍历是否只选中一个
/// </summary>
private bool CheckLV(ref string m_message)
{
int i = 0;
foreach (ListViewItem lv in LVShowRecord.Items)
{
if (lv.Checked)
{
i++;
}
}
if (i < 1)
{
m_message = "请选择文件!";
}
if (i > 1)
{
m_message = "一次只能选择一个文件!";
}
return i == 1 ? true : false;
}
#endregion
Code
录音计时#region 录音计时
private void timer1_Tick(object sender, EventArgs e)
{
try
{
StringToTime(labelViewTime.Text);
this.trackBar1.Minimum = 0;
this.trackBar1.Maximum = 2;
this.trackBar1.TickFrequency = 1;
if (recorder.Recording)
{
if (trackBar1.Value == 2)
{
trackBar1.Value = 0;
}
trackBar1.Value++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
public void StringToTime(string strTime)
{
int minutes = Convert.ToInt32(strTime.Substring(0, strTime.IndexOf(":")));
int seconds = Convert.ToInt32(strTime.Substring(strTime.IndexOf(":") + 1));
RunTime(minutes, seconds);
}
public void RunTime(int minutes, int seconds)
{
try
{
seconds++;
if (seconds > 59)
{
minutes++;
seconds = 0;
if (minutes > 59)
{
recorder.Stop();
MessageBox.Show("时间超过!");
}
}
string min = minutes < 10 ? ("0" + minutes.ToString()) : minutes.ToString();
string sec = seconds < 10 ? ("0" + seconds.ToString()) : seconds.ToString();
labelViewTime.Text = min + ":" + sec;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
#endregion
录音计时#region 录音计时
private void timer1_Tick(object sender, EventArgs e)
{
try
{
StringToTime(labelViewTime.Text);
this.trackBar1.Minimum = 0;
this.trackBar1.Maximum = 2;
this.trackBar1.TickFrequency = 1;
if (recorder.Recording)
{
if (trackBar1.Value == 2)
{
trackBar1.Value = 0;
}
trackBar1.Value++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
public void StringToTime(string strTime)
{
int minutes = Convert.ToInt32(strTime.Substring(0, strTime.IndexOf(":")));
int seconds = Convert.ToInt32(strTime.Substring(strTime.IndexOf(":") + 1));
RunTime(minutes, seconds);
}
public void RunTime(int minutes, int seconds)
{
try
{
seconds++;
if (seconds > 59)
{
minutes++;
seconds = 0;
if (minutes > 59)
{
recorder.Stop();
MessageBox.Show("时间超过!");
}
}
string min = minutes < 10 ? ("0" + minutes.ToString()) : minutes.ToString();
string sec = seconds < 10 ? ("0" + seconds.ToString()) : seconds.ToString();
labelViewTime.Text = min + ":" + sec;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
#endregion
Code
#region 进度条
private void timer2_Tick(object sender, EventArgs e)
{
try
{
StringToTime(labelViewTime.Text);
this.trackBar1.Minimum = 0;
int filelen = ((int)filelength) - 1;
this.trackBar1.Maximum = filelen;
this.trackBar1.TickFrequency = 1;
if (player.Playing)
{
if (trackBar1.Value == (int)filelen)
{
player.Stop();
trackBar1.Value = 0;
labelViewTime.Text = "00:00";
imageList2.ImageSize = new Size(52, 56);
btnPlay.Image = imageList2.Images[0];
labelState.Text = "停止";
btnPlay.Enabled = true;
btnRecord.Enabled = true;
btnStop.Enabled = true;
btnOpenFile.Enabled = true;
pauseAndRestart = 0;
ChangeFile = 0;
}
trackBar1.Value++;
}
else
{
trackBar1.Value = 0;
timer2.Enabled = false;
labelViewTime.Text = "00:00";
imageList2.ImageSize = new Size(52, 56);
btnPlay.Image = imageList2.Images[0];
labelState.Text = "停止";
btnDel.Enabled = true;
btnPlay.Enabled = true;
btnRecord.Enabled = true;
btnOpenFile.Enabled = true;
btnStop.Enabled = true;
btnOpenFile.Enabled = true;
ChangeFile = 0;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
#endregion
#region 进度条
private void timer2_Tick(object sender, EventArgs e)
{
try
{
StringToTime(labelViewTime.Text);
this.trackBar1.Minimum = 0;
int filelen = ((int)filelength) - 1;
this.trackBar1.Maximum = filelen;
this.trackBar1.TickFrequency = 1;
if (player.Playing)
{
if (trackBar1.Value == (int)filelen)
{
player.Stop();
trackBar1.Value = 0;
labelViewTime.Text = "00:00";
imageList2.ImageSize = new Size(52, 56);
btnPlay.Image = imageList2.Images[0];
labelState.Text = "停止";
btnPlay.Enabled = true;
btnRecord.Enabled = true;
btnStop.Enabled = true;
btnOpenFile.Enabled = true;
pauseAndRestart = 0;
ChangeFile = 0;
}
trackBar1.Value++;
}
else
{
trackBar1.Value = 0;
timer2.Enabled = false;
labelViewTime.Text = "00:00";
imageList2.ImageSize = new Size(52, 56);
btnPlay.Image = imageList2.Images[0];
labelState.Text = "停止";
btnDel.Enabled = true;
btnPlay.Enabled = true;
btnRecord.Enabled = true;
btnOpenFile.Enabled = true;
btnStop.Enabled = true;
btnOpenFile.Enabled = true;
ChangeFile = 0;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
}
}
#endregion
这个组件有个比较大的不足就是在播放时如果要停止执行到player.stop()时就会出错,我到官方网站上也有人提了类似的问题可是并没有找到解决办法,后来只好用暂停的事件去代替,不过总算不再出现异常 。
虽然有些手机都有自带的录音机没有也可以安装,不过或许有些程序总是会用到的,哈哈,希望对园里这方面开发的朋友会有帮助。