分享一个优先级声音播放类
2010-09-27 21:17
357 查看
SPlayer
1 class SPlayer
2 {
3 private static SoundPriorities _lastPriority = SoundPriorities.Low;
4
5 public static bool Play(string wavPath, SoundPriorities priority)
6 {
7 if (string.IsNullOrEmpty(wavPath)) return false;
8 if (!File.Exists(wavPath)) return false;
9 if (!wavPath.EndsWith(".wav", StringComparison.OrdinalIgnoreCase)) return false;
10
11 if (priority >= _lastPriority)
12 {
13 Stop();
14 _lastPriority = priority;
15 return NativeMethods.PlaySoundA(wavPath, new IntPtr(), (int)(PlaySoundFlags.SND_ASYNC | PlaySoundFlags.SND_LOOP));
16 }
17
18 return false;
19 }
20
21 public static void Stop()
22 {
23 if (NativeMethods.PlaySoundA(null, new IntPtr(), 0))
24 _lastPriority = SoundPriorities.Low;
25 }
26 }
27
28 [Flags]
29 enum PlaySoundFlags : int
30 {
31 SND_SYNC = 0x0000,
32 SND_ASYNC = 0x0001,
33 SND_NODEFAULT = 0x0002,
34 SND_LOOP = 0x0008,
35 SND_NOSTOP = 0x0010,
36 SND_NOWAIT = 0x00002000,
37 SND_FILENAME = 0x00020000,
38 SND_RESOURCE = 0x00040004
39 }
40
41 enum SoundPriorities : int
42 {
43 Low = 0,
44 Normal = 1,
45 High = 2
46 }
47
48 [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
49 public struct HINSTANCE__
50 {
51
52 /// int
53 public int unused;
54 }
55
56 public partial class NativeMethods
57 {
58
59 /// Return Type: BOOL->int
60 ///pszSound: LPCSTR->CHAR*
61 ///hmod: HMODULE->HINSTANCE->HINSTANCE__*
62 ///fdwSound: DWORD->unsigned int
63 [System.Runtime.InteropServices.DllImportAttribute("winmm.dll", EntryPoint = "PlaySoundA")]
64 [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
65 public static extern bool PlaySoundA([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string pszSound, System.IntPtr hmod, uint fdwSound);
66
67 }
雕虫小技啊,不足挂齿
,唯一要注意的是:
根据msdn文档所描述,该api(winmm:PlaySound)播放任意新声音文件会自动停止之前已播放的内容,所以...
当然这个类还可以写的更复杂,这个就不说了
1 class SPlayer
2 {
3 private static SoundPriorities _lastPriority = SoundPriorities.Low;
4
5 public static bool Play(string wavPath, SoundPriorities priority)
6 {
7 if (string.IsNullOrEmpty(wavPath)) return false;
8 if (!File.Exists(wavPath)) return false;
9 if (!wavPath.EndsWith(".wav", StringComparison.OrdinalIgnoreCase)) return false;
10
11 if (priority >= _lastPriority)
12 {
13 Stop();
14 _lastPriority = priority;
15 return NativeMethods.PlaySoundA(wavPath, new IntPtr(), (int)(PlaySoundFlags.SND_ASYNC | PlaySoundFlags.SND_LOOP));
16 }
17
18 return false;
19 }
20
21 public static void Stop()
22 {
23 if (NativeMethods.PlaySoundA(null, new IntPtr(), 0))
24 _lastPriority = SoundPriorities.Low;
25 }
26 }
27
28 [Flags]
29 enum PlaySoundFlags : int
30 {
31 SND_SYNC = 0x0000,
32 SND_ASYNC = 0x0001,
33 SND_NODEFAULT = 0x0002,
34 SND_LOOP = 0x0008,
35 SND_NOSTOP = 0x0010,
36 SND_NOWAIT = 0x00002000,
37 SND_FILENAME = 0x00020000,
38 SND_RESOURCE = 0x00040004
39 }
40
41 enum SoundPriorities : int
42 {
43 Low = 0,
44 Normal = 1,
45 High = 2
46 }
47
48 [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
49 public struct HINSTANCE__
50 {
51
52 /// int
53 public int unused;
54 }
55
56 public partial class NativeMethods
57 {
58
59 /// Return Type: BOOL->int
60 ///pszSound: LPCSTR->CHAR*
61 ///hmod: HMODULE->HINSTANCE->HINSTANCE__*
62 ///fdwSound: DWORD->unsigned int
63 [System.Runtime.InteropServices.DllImportAttribute("winmm.dll", EntryPoint = "PlaySoundA")]
64 [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
65 public static extern bool PlaySoundA([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPStr)] string pszSound, System.IntPtr hmod, uint fdwSound);
66
67 }
雕虫小技啊,不足挂齿
,唯一要注意的是:
根据msdn文档所描述,该api(winmm:PlaySound)播放任意新声音文件会自动停止之前已播放的内容,所以...
当然这个类还可以写的更复杂,这个就不说了
相关文章推荐
- WinAPI: MessageBeep - 播放一个系统声音
- IDirectSoundBuffer Play 播放一个很短的声音,发现总是不结束
- 分享一个 今天发现一个 ckplayer 播放rtmp 问题 问题。
- Android使用的webcview中带有音乐播放控件,在关闭或分享时处于界面不可见状态下,声音仍在播放的问题解决
- Android使用的webcview中带有音乐播放控件,在关闭或分享时处于界面不可见状态下,声音仍在播放的问题解决
- 【分享】4412开发板ubuntu 12.0.4播放音乐没有声音解决方法
- Android AudioManager处理两个播放器同时有声音,停止其中一个播放的问题
- 分享一个视频播放库
- WinAPI: MessageBeep - 播放一个系统声音
- 转: WebView载入一个网页 但是退出对应的activity时, 声音、视频无法停止播放 解决方案(未验证)
- Swift - 制作一个录音机(声音的录制与播放)
- Android AudioManager处理两个播放器同时有声音,停止其中一个播放的问题
- Delphi播放声音文件类(包括调整其中任意一个声音的大小)
- Android AudioManager处理两个播放器同时有声音,停止其中一个播放的问题,暂停其他的播放
- Swift - 制作一个录音机(声音的录制与播放)
- 分享一个漂亮的自动播放的Tabs
- 分享一个flash播放音乐的类
- 在编写flash游戏播放声音时的一个要注意的地方
- 实现Android播放声音资源的一个简单的工具类
- 分享一段利用JavaScript实现播放声音/音乐的例子。