- 苏萦
-
1 public struct Mp3Info
2
3 {
4
5 public string identify;//TAG,三个字节
6
7 public string Title;//歌曲名,30个字节
8
9 public string Artist;//歌手名,30个字节
10
11 public string Album;//所属唱片,30个字节
12
13 public string Year;//年,4个字符
14
15 public string Comment;//注释,28个字节
16
17
18
19 public char reserved1;//保留位,一个字节
20
21 public char reserved2;//保留位,一个字节
22
23 public char reserved3;//保留位,一个字节
24
25 }
26
27
28 /// <summary>
29
30 /// 获取MP3文件最后128个字节
31
32 /// </summary>
33
34 /// <param name="FileName">文件名</param>
35
36 /// <returns>返回字节数组</returns>
37
38 private byte[] getLast128(string FileName)
39
40 {
41
42 FileStream fs = new FileStream(FileName,FileMode.Open,FileAccess.Read);
43
44 string title = ReadAuthor(fs);
45
46 Stream stream = fs;
47
48
49
50 stream.Seek(-300,SeekOrigin.End);
51
52
53
54 const int seekPos = 300;
55
56 int rl = 0;
57
58 byte[] Info = new byte[seekPos];
59
60 rl = stream.Read(Info,0,seekPos);
61
62
63
64 fs.Close();
65
66 stream.Close();
67
68
69
70 return Info;
71
72 }
73
74 private string ReadAuthor(Stream binary_file)
75 {
76 System.Text.Encoding encoding = System.Text.Encoding.UTF8;
77 // Read string from binary file with UTF8 encoding
78 byte[] buffer = new byte[30];
79 binary_file.Read(buffer, 0, 30);
80 return encoding.GetString(buffer);
81 }
82
83 /// <summary>
84
85 /// 获取MP3歌曲的相关信息
86
87 /// </summary>
88
89 /// <param name = "Info">从MP3文件中截取的二进制信息</param>
90
91 /// <returns>返回一个Mp3Info结构</returns>
92
93 private Mp3Info getMp3Info(byte[] Info)
94
95 {
96
97 Mp3Info mp3Info = new Mp3Info();
98
99
100
101 string str = null;
102
103 int i;
104
105 int position = 0;//循环的起始值
106
107 int currentIndex = 0;//Info的当前索引值
108
109 //获取TAG标识
110
111 for(i = currentIndex;i<currentIndex+3;i++)
112
113 {
114
115 str = str+(char)Info[i];
116
117
118
119 position++;
120
121 }
122
123 currentIndex = position;
124
125 mp3Info.identify = str;
126
127
128
129 //获取歌名
130
131 str = null;
132
133 byte[] bytTitle = new byte[30];//将歌名部分读到一个单独的数组中
134
135 int j = 0;
136
137 for(i = currentIndex;i<currentIndex+30;i++)
138
139 {
140
141 bytTitle[j] = Info[i];
142
143 position++;
144
145 j++;
146
147 }
148
149 currentIndex = position;
150
151 mp3Info.Title = this.byteToString(bytTitle);
152
153
154
155 //获取歌手名
156
157 str = null;
158
159 j = 0;
160
161 byte[] bytArtist = new byte[30];//将歌手名部分读到一个单独的数组中
162
163 for(i = currentIndex;i<currentIndex+30;i++)
164
165 {
166
167 bytArtist[j] = Info[i];
168
169 position++;
170
171 j++;
172
173 }
174
175 currentIndex = position;
176
177 mp3Info.Artist = this.byteToString(bytArtist);
178
179
180
181 //获取唱片名
182
183 str = null;
184
185 j = 0;
186
187 byte[] bytAlbum = new byte[30];//将唱片名部分读到一个单独的数组中
188
189 for(i = currentIndex;i<currentIndex+30;i++)
190
191 {
192
193 bytAlbum[j] = Info[i];
194
195 position++;
196
197 j++;
198
199 }
200
201 currentIndex = position;
202
203 mp3Info.Album = this.byteToString(bytAlbum);
204
205
206
207 //获取年
208
209 str = null;
210
211 j = 0;
212
213 byte[] bytYear = new byte[4];//将年部分读到一个单独的数组中
214
215 for(i = currentIndex;i<currentIndex+4;i++)
216
217 {
218
219 bytYear[j] = Info[i];
220
221 position++;
222
223 j++;
224
225 }
226
227 currentIndex = position;
228
229 mp3Info.Year = this.byteToString(bytYear);
230
231
232
233 //获取注释
234
235 str = null;
236
237 j = 0;
238
239 byte[] bytComment = new byte[28];//将注释部分读到一个单独的数组中
240
241 for(i = currentIndex;i<currentIndex+25;i++)
242
243 {
244
245 bytComment[j] = Info[i];
246
247 position++;
248
249 j++;
250
251 }
252
253 currentIndex = position;
254
255 mp3Info.Comment = this.byteToString(bytComment);
256
257
258
259 //以下获取保留位
260
261 mp3Info.reserved1 = (char)Info[++position];
262
263 mp3Info.reserved2 = (char)Info[++position];
264
265 mp3Info.reserved3 = (char)Info[++position];
266
267
268
269 return mp3Info;
270 }
271
272 /// <summary>
273
274 /// 将字节数组转换成字符串
275
276 /// </summary>
277
278 /// <param name = "b">字节数组</param>
279
280 /// <returns>返回转换后的字符串</returns>
281
282 private string byteToString(byte[] b)
283
284 {
285 Encoding enc = Encoding.GetEncoding("GB2312");
286
287
288 string str = enc.GetString(b);
289
290 str = str.Substring(0,str.IndexOf(" ") >= 0 ? str.IndexOf(" ") : str.Length);//去掉无用字符
291
292
293
294 return str;
295
296 }