Pocket PC 录音
1
作者:ah, 2007-10-31
9
using System;
10
using System.Drawing;
11
using System.Collections;
12
using System.Windows.Forms;
13
using System.Data;
14
using OpenNETCF.Multimedia.Audio;
15
using System.IO;
16
using System.Reflection;
17
18
namespace ppcvoicerecorder
19
{
20
public partial class frmRecorder : Form
21
{
22
private Player player;
23
private Recorder recorder;
24
private Stream stream;
25
private int timeLeft;
26
27
private const int RECORD_LENGTH = 600;
28
private const int MAX_RECORD_LENGTH = 600;//录音最大长度
29
private string TEMP_PATH = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
30
private string TEMP_FILE = SystemInfoFilePath();
31
private DateTime STARTRECORD_TIME;//开始录音时间
32
private DateTime ENDRECORE_TIME;//结实录音时间
33
ConnDate conn = new ConnDate();
34
35
public frmRecorder()
36
{
37
InitializeComponent();
38
39
lblTime.Width = this.Width;
40
// 实例化录音机
41
recorder = new Recorder();
42
recorder.DoneRecording += new WaveFinishedHandler(recorder_DoneRecording);
43
// 实例化播放器
44
player = new Player();
45
player.DonePlaying += new WaveDoneHandler(player_DonePlaying);
46
}
47
48
49
void recorder_DoneRecording()
50
{
51
mniRecord.Text = "录音";
52
tmrRecord.Enabled = false;
53
54
}
55
56
设置本地存储路径
68
69
private DialogResult _isCancel = DialogResult.No;
70
71
public DialogResult IsCancel
72
{
73
get { return _isCancel; }
74
}
75
76
void player_DonePlaying(object sender, IntPtr wParam, IntPtr lParam)
77
{
78
// mniPlay.Text = "播放";
79
}
80
81
private void tmrRecord_Tick(object sender, EventArgs e)
82
{
83
timeLeft--;
84
if (timeLeft >0)
85
{
86
lblTime.Text = string.Format("0:{0:00}", MAX_RECORD_LENGTH - timeLeft);
87
}
88
}
89
90
private Stream PrepareTempFile()
91
{
92
// 检查临时目录是否存在
93
if (!Directory.Exists(TEMP_PATH))
94
{
95
Directory.CreateDirectory(TEMP_PATH);
96
}
97
98
if (File.Exists(TEMP_FILE))
99
{
100
File.Delete(TEMP_FILE);
101
}
102
103
stream = File.OpenWrite(TEMP_FILE);
104
return stream;
105
}
106
107
private void Record()
108
{
109
try
110
{
111
if (mniRecord.Text == "停止/确认")
112
{
113
tmrRecord.Enabled = false;
114
// 停止录音
115
this.ENDRECORE_TIME = DateTime.Now;
116
this.lblTime.Text = conn.DateDiff(this.STARTRECORD_TIME,this.ENDRECORE_TIME);
117
recorder.Stop();
118
_isCancel = DialogResult.OK;
119
mniRecord.Text = "录音";
120
this.Close();
121
}
122
else
123
{
124
this.STARTRECORD_TIME = DateTime.Now;
125
stream = PrepareTempFile();
126
timeLeft = RECORD_LENGTH;
127
tmrRecord.Enabled = true;
128
129
// 开始录音
130
recorder.RecordFor(stream, RECORD_LENGTH, SoundFormats.Mono8bit11kHz);
131
mniRecord.Text = "停止/确认";
132
}
133
}
134
catch (Exception ex)
135
{
136
MessageBox.Show(ex.Message);
137
}
138
}
139
140
/// <summary>
141
/// 取得录音文件路径
142
/// </summary>
143
/// <returns>返回路径</returns>
144
public string GetFileInfo()
145
{
146
try
147
{
148
return this.TEMP_FILE;
149
}
150
catch (Exception err)
151
{
152
return string.Empty;
153
MessageBox.Show(err.Message);
154
}
155
}
156
157
private void mniRecord_Click(object sender, EventArgs e)
158
{
159
this.Record();
160
}
161
162
private void mniMenuBack_Click(object sender, EventArgs e)
163
{
164
this.Close();
165
}
166
167
}
168
}
作者:ah, 2007-10-319
using System;10
using System.Drawing;11
using System.Collections;12
using System.Windows.Forms;13
using System.Data;14
using OpenNETCF.Multimedia.Audio;15
using System.IO;16
using System.Reflection;17

18
namespace ppcvoicerecorder19
{20
public partial class frmRecorder : Form 21
{ 22
private Player player;23
private Recorder recorder;24
private Stream stream;25
private int timeLeft;26
27
private const int RECORD_LENGTH = 600;28
private const int MAX_RECORD_LENGTH = 600;//录音最大长度29
private string TEMP_PATH = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);30
private string TEMP_FILE = SystemInfoFilePath();31
private DateTime STARTRECORD_TIME;//开始录音时间32
private DateTime ENDRECORE_TIME;//结实录音时间33
ConnDate conn = new ConnDate();34

35
public frmRecorder()36
{37
InitializeComponent();38

39
lblTime.Width = this.Width; 40
// 实例化录音机41
recorder = new Recorder();42
recorder.DoneRecording += new WaveFinishedHandler(recorder_DoneRecording);43
// 实例化播放器44
player = new Player();45
player.DonePlaying += new WaveDoneHandler(player_DonePlaying); 46
}47
48

49
void recorder_DoneRecording()50
{51
mniRecord.Text = "录音";52
tmrRecord.Enabled = false;53
54
}55

56
设置本地存储路径68

69
private DialogResult _isCancel = DialogResult.No;70

71
public DialogResult IsCancel72
{73
get { return _isCancel; }74
} 75

76
void player_DonePlaying(object sender, IntPtr wParam, IntPtr lParam)77
{78
// mniPlay.Text = "播放";79
}80

81
private void tmrRecord_Tick(object sender, EventArgs e)82
{83
timeLeft--;84
if (timeLeft >0)85
{86
lblTime.Text = string.Format("0:{0:00}", MAX_RECORD_LENGTH - timeLeft);87
}88
} 89
90
private Stream PrepareTempFile()91
{92
// 检查临时目录是否存在93
if (!Directory.Exists(TEMP_PATH))94
{95
Directory.CreateDirectory(TEMP_PATH);96
} 97

98
if (File.Exists(TEMP_FILE))99
{100
File.Delete(TEMP_FILE);101
}102

103
stream = File.OpenWrite(TEMP_FILE);104
return stream;105
} 106

107
private void Record()108
{109
try110
{111
if (mniRecord.Text == "停止/确认")112
{113
tmrRecord.Enabled = false; 114
// 停止录音115
this.ENDRECORE_TIME = DateTime.Now;116
this.lblTime.Text = conn.DateDiff(this.STARTRECORD_TIME,this.ENDRECORE_TIME);117
recorder.Stop();118
_isCancel = DialogResult.OK;119
mniRecord.Text = "录音"; 120
this.Close(); 121
}122
else123
{124
this.STARTRECORD_TIME = DateTime.Now;125
stream = PrepareTempFile(); 126
timeLeft = RECORD_LENGTH;127
tmrRecord.Enabled = true;128

129
// 开始录音130
recorder.RecordFor(stream, RECORD_LENGTH, SoundFormats.Mono8bit11kHz);131
mniRecord.Text = "停止/确认"; 132
}133
}134
catch (Exception ex)135
{136
MessageBox.Show(ex.Message);137
}138
} 139

140
/// <summary>141
/// 取得录音文件路径142
/// </summary>143
/// <returns>返回路径</returns>144
public string GetFileInfo()145
{146
try147
{148
return this.TEMP_FILE;149
}150
catch (Exception err)151
{152
return string.Empty;153
MessageBox.Show(err.Message);154
}155
} 156

157
private void mniRecord_Click(object sender, EventArgs e)158
{159
this.Record(); 160
} 161

162
private void mniMenuBack_Click(object sender, EventArgs e)163
{164
this.Close();165
} 166
167
}168
}
为成功找方法,不为失败找借口!


浙公网安备 33010602011771号