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
}

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

为成功找方法,不为失败找借口!