文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>分享一个优先级声音播放类

分享一个优先级声音播放类

时间:2010-09-27  来源:micYng

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)播放任意新声音文件会自动停止之前已播放的内容,所以...

 

当然这个类还可以写的更复杂,这个就不说了

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载