-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundPlayer.cs
89 lines (72 loc) · 2.33 KB
/
SoundPlayer.cs
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
using System;
using System.Threading;
using System.Runtime.InteropServices;
using System.Reflection;
using System.IO;
namespace eggtimer
{
/// <summary>
/// Plays a .wav file asynchronously
/// </summary>
public class FileSoundPlayer
{
string sound;
public FileSoundPlayer(string newsound)
{
sound = newsound;
}
public void play()
{
Thread playthread = new Thread(new ThreadStart(player));
playthread.Start();
}
[DllImport("winmm.dll")]
private static extern long PlaySound(string sName, uint hModule, uint dwFlags);
private void player()
{
PlaySound(sound, 0, 0);
}
}
/// <summary>
/// Plays a .wav file from an embedded resource asynchronously
/// </summary>
public class ResourceSoundPlayer
{
byte[] soundbytes;
public ResourceSoundPlayer(string sound)
{
Stream s=Assembly.GetExecutingAssembly().GetManifestResourceStream(sound);
soundbytes = new Byte[s.Length];
s.Read(soundbytes, 0, (int) s.Length);
}
public ResourceSoundPlayer(System.Type type, string sound)
{
Stream s=Assembly.GetExecutingAssembly().GetManifestResourceStream(type, sound);
soundbytes = new Byte[s.Length];
s.Read(soundbytes, 0, (int) s.Length);
}
public void play_async()
{
Thread playthread = new Thread(new ThreadStart(player));
playthread.Start();
}
public void play_sync()
{
player();
}
[DllImport("winmm.dll")]
public static extern bool sndPlaySound(byte[] soundbytes, int param);
[DllImport("winmm.dll")]
private static extern long PlaySound(string sName, uint hModule, uint dwFlags);
private void player()
{
const int SND_SYNC = 0; // Play synchronously (default).
//const int SND_ASYNC = 0x1; // Play asynchronously (see note below).
const int SND_NODEFAULT = 0x2; // Do not use default sound.
const int SND_MEMORY = 0x4; // lpszSoundName points to a memory file.
//const int SND_LOOP = 0x8; // Loop the sound until next sndPlaySound.
//const int SND_NOSTOP = 0x10; // Do not stop any currently playing sound.
sndPlaySound(soundbytes, SND_SYNC | SND_MEMORY | SND_NODEFAULT);
}
}
}