1
2
3
4
5
6
7
8
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
57
58
59
60
61
62
63
64
65
66
67
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
// **********************************************
// Dies ist eines der 322 Rezepte aus dem
// C# 2005 Premium Codebook.
// Das Snippet wurde vom Autor mit freundlicher
// Genehmigung von Addison Wesley
// bei dotnet-snippets.de veröffentlicht.
// **********************************************
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
/// <summary>
/// Repräsentiert eine MCI-Ausnahme
/// </summary>
public class MciException : Exception
{
public MciException(string message)
{
}
}
/// <summary>
/// Klasse für das Abspielen von Mediadateien über MCI
/// </summary>
public class Mci : IDisposable
{
/* Deklaration der MCI-Funktionen, -Konstanten und -Strukturen */
[DllImport("winmm.dll", CharSet = CharSet.Auto)]
private static extern int mciSendString(string lpstrCommand,
StringBuilder lpstrReturnString, int uReturnLength, IntPtr
hwndCallback);
[DllImport("winmm.dll", CharSet = CharSet.Auto)]
private static extern int mciGetErrorString(int dwError, StringBuilder
lpstrBuffer, int uLength);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern int GetShortPathName(string lpszLongPath,
StringBuilder lpszShortPath, int cchBuffer);
/* Eigenschaft für den MCI-Alias */
private string alias;
private bool isOpen = false;
/// <summary>
/// Gibt an, ob gerade ein MCI-Gerät geöffnet ist
/// </summary>
public bool IsOpen
{
get
{
return this.isOpen;
}
}
/// <summary>
/// Liefert eine Fehlermeldung zu einem gegebenen
/// Fehlercode
/// </summary>
/// <param name="errorCode">Der Fehlercode</param>
/// <returns>Gibt die Fehlermeldung zurück</returns>
private string GetMciError(int errorCode)
{
StringBuilder errorMessage = new StringBuilder(255);
if (mciGetErrorString(errorCode, errorMessage,
errorMessage.Capacity) == 0)
{
return "MCI-Fehler " + errorCode;
}
else
{
return errorMessage.ToString();
}
}
/// <summary>
/// Öffnet eine Multimediadatei
/// </summary>
/// <param name="filename">Der Dateiname</param>
public void Open(string filename)
{
Open(filename, null);
}
/// <summary>
/// Öffnet eine Multimediadatei
/// </summary>
/// <param name="filename">Der Dateiname</param>
/// <param name="owner">Der Besitzer der erzeugten MCI-Instanz</param>
public void Open(string filename, Control owner)
{
// Schließen einer eventuell zuvor noch geöffneten
// Datei
if (this.IsOpen)
{
this.Close();
}
// Alias für das MCI-Gerät erzeugen
this.alias = Guid.NewGuid().ToString("N");
// Überprüfen, ob die Datei existiert
if (File.Exists(filename) == false)
{
throw new FileNotFoundException("Die Datei '" + filename +
"' existiert nicht", filename);
}
// Den kurzen Dateinamen ermitteln
StringBuilder shortPath = new StringBuilder(261);
if (GetShortPathName(filename, shortPath, shortPath.Capacity) == 0)
{
throw new MciException("Fehler beim Auslesen des kurzen " +
"Dateinamens für '" + filename + "': Windows-Fehler " +
Marshal.GetLastWin32Error());
}
// MCI-Befehlsstring zum Öffnen zusammensetzen
string mciString = "open " + shortPath.ToString() +
" type mpegvideo alias " + this.alias;
if (owner != null)
{
mciString += " parent " + (int)owner.Handle + " style child";
}
// MCI-Gerät öffnen
int result = mciSendString(mciString, null, 0, IntPtr.Zero);
if (result != 0)
{
throw new MciException("Fehler beim Öffnen des MCI-Geräts: " +
GetMciError(result));
}
this.isOpen = true;
// Das Zeitformat für Längen- und Positionsangaben explizit
// auf Millisekunden setzen
mciString = "set " + this.alias + " time format ms";
result = mciSendString(mciString, null, 0, IntPtr.Zero);
if (result != 0)
{
throw new MciException("Fehler beim Setzen des Zeitformats: " +
GetMciError(result));
}
}
/// <summary>
/// Gibt die Abspiellänge der aktuellen Multimediadatei zurück
/// </summary>
public int Length
{
get
{
StringBuilder buffer = new StringBuilder(261);
int result = mciSendString("status " + this.alias + " length",
buffer, buffer.Capacity, IntPtr.Zero);
if (result != 0)
{
throw new MciException("Fehler beim Lesen von 'Length': " +
GetMciError(result));
}
return int.Parse(buffer.ToString());
}
}
/// <summary>
/// Spielt eine geladene Multimediadatei ab
/// </summary>
/// <param name="repeat">Gibt an, ob das Abspielen nach dem Ende wieder von vorne beginnen soll</param>
public void Play(bool repeat)
{
Play(0, this.Length, repeat);
}
/// <summary>
/// Spielt eine geladene Multimediadatei ab einer bestimmten Position ab
/// </summary>
/// <param name="from">Die Startposition</param>
/// <param name="repeat">Gibt an, ob das Abspielen nach dem Ende wieder von vorne beginnen soll</param>
public void Play(int from, bool repeat)
{
Play(from, this.Length - from, repeat);
}
/// <summary>
/// Spielt eine geladene Multimediadatei ab einer bestimmten Position
/// und bis zu einer bestimmten anderen Position ab
/// </summary>
/// <param name="from">Die Startposition</param>
/// <param name="to">Die Endposition</param>
/// <param name="repeat">Gibt an, ob das Abspielen nach dem Ende wieder von vorne beginnen soll</param>
public void Play(int from, int to, bool repeat)
{
string mciString = "play " + this.alias +
" from " + from + " to " + to;
if (repeat)
{
mciString += " repeat";
}
int result = mciSendString(mciString, null, 0, IntPtr.Zero);
if (result != 0)
{
throw new MciException("Fehler beim Aufruf von 'Play': " +
GetMciError(result));
}
}
/// <summary>
/// Verwaltet die Lautstärke
/// </summary>
public int Volume
{
get
{
StringBuilder buffer = new StringBuilder(261);
int result = mciSendString("status " + this.alias + " volume",
buffer, buffer.Capacity, IntPtr.Zero);
if (result != 0)
{
throw new MciException("Fehler beim Lesen von 'Volume': " +
GetMciError(result));
}
return int.Parse(buffer.ToString());
}
set
{
int result = mciSendString("setaudio " + this.alias +
" volume to " + value, null, 0, IntPtr.Zero);
if (result != 0)
{
throw new MciException("Fehler beim Aufruf von 'SetAudio': " +
GetMciError(result));
}
}
}
/// <summary>
/// Verwaltet die aktuelle Position in den Multimedia-Daten
/// </summary>
public int Position
{
get
{
StringBuilder buffer = new StringBuilder(261);
int result = mciSendString("status " + this.alias + " position",
buffer, buffer.Capacity, IntPtr.Zero);
if (result != 0)
{
throw new MciException("Fehler beim Lesen von 'Position': " +
GetMciError(result));
}
return int.Parse(buffer.ToString());
}
set
{
int result = mciSendString("seek " + this.alias +
" to " + value, null, 0, IntPtr.Zero);
if (result != 0)
{
throw new MciException("Fehler beim Setzen von 'Position'" +
GetMciError(result));
}
result = mciSendString("play " + this.alias, null, 0, IntPtr.Zero);
if (result != 0)
{
throw new MciException("Fehler beim Aufruf von 'Play': " +
GetMciError(result));
}
}
}
/// <summary>
/// Verwaltet die aktuelle Abspiel-Geschwindigkeit
/// </summary>
public int PlaybackSpeed
{
get
{
StringBuilder buffer = new StringBuilder(261);
int result = mciSendString("status " + this.alias + " speed",
buffer, buffer.Capacity, IntPtr.Zero);
if (result != 0)
{
throw new MciException("Fehler beim Lesen von 'Speed'" +
GetMciError(result));
}
return int.Parse(buffer.ToString());
}
set
{
string mciString = "set " + this.alias + " speed " + value;
int result = mciSendString(mciString, null, 0, IntPtr.Zero);
if (result != 0)
{
throw new MciException("Fehler beim Setzen von 'Speed': " +
GetMciError(result));
}
}
}
/// <summary>
/// Methode zur Definition der Abspielposition und -größe für Videos, die auf einem Formular, einer PictureBox o. ä. dargestellt werden
/// </summary>
/// <param name="x">Die X-Position</param>
/// <param name="y">Die Y-Position</param>
/// <param name="width">Die Breite</param>
/// <param name="height">Die Höhe</param>
public void SetRectangle(int x, int y, int width, int height)
{
string mciString = "put " + this.alias + " window at " + x +
" " + y + " " + width + " " + height;
int result = mciSendString(mciString, null, 0, IntPtr.Zero);
if (result != 0)
{
throw new MciException("Fehler beim Aufruf von 'Put Window at': " +
GetMciError(result));
}
}
/// <summary>
/// Stoppt den aktuellen Abspielvorgang
/// </summary>
public void Stop()
{
string mciString = "stop " + this.alias;
int result = mciSendString(mciString, null, 0, IntPtr.Zero);
if (result != 0)
{
throw new MciException("Fehler beim Aufruf von 'Stop': " +
GetMciError(result));
}
}
/// <summary>
/// Schließt ein aktuell geöffnetes MCI-Gerät
/// </summary>
public void Close()
{
if (this.isOpen)
{
string mciString = "close " + this.alias;
int result = mciSendString(mciString, null, 0, IntPtr.Zero);
if (result != 0)
{
throw new MciException("Fehler beim Aufruf von 'Close': " +
GetMciError(result));
}
this.isOpen = false;
}
}
/// <summary>
/// Schließt ein aktuell geöffnetes MCI-Gerät
/// </summary>
public void Dispose()
{
this.Close();
}
/// <summary>
/// Finalisierer
/// </summary>
~Mci()
{
this.Close();
}
}
|