C#中如何打开wav文件
展开全部
using System.Runtime.InteropServices;
class Play
{
[DllImport("WinMM.dll")]
public static extern bool PlaySound(string szSound, IntPtr hMod, PlaySoundFlags flags);
[Flags]
public enum PlaySoundFlags : int
{
SND_SYNC = 0x0000, /* play synchronously (default) */ //同步
SND_ASYNC = 0x0001, /* play asynchronously */ //异步
SND_NODEFAULT = 0x0002, /* silence (!default) if sound not found */
SND_MEMORY = 0x0004, /* pszSound points to a memory file */
SND_LOOP = 0x0008, /* loop the sound until next sndPlaySound */
SND_NOSTOP = 0x0010, /* don't stop any currently playing sound */
SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
SND_ALIAS = 0x00010000, /* name is a registry alias */
SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */
SND_FILENAME = 0x00020000, /* name is file name */
SND_RESOURCE = 0x00040004 /* name is resource name or atom */
}
public static void PlayWav(string strPath)
{
PlaySound(strPath, IntPtr.Zero, PlaySoundFlags.SND_ASYNC);
}
}
class Play
{
[DllImport("WinMM.dll")]
public static extern bool PlaySound(string szSound, IntPtr hMod, PlaySoundFlags flags);
[Flags]
public enum PlaySoundFlags : int
{
SND_SYNC = 0x0000, /* play synchronously (default) */ //同步
SND_ASYNC = 0x0001, /* play asynchronously */ //异步
SND_NODEFAULT = 0x0002, /* silence (!default) if sound not found */
SND_MEMORY = 0x0004, /* pszSound points to a memory file */
SND_LOOP = 0x0008, /* loop the sound until next sndPlaySound */
SND_NOSTOP = 0x0010, /* don't stop any currently playing sound */
SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
SND_ALIAS = 0x00010000, /* name is a registry alias */
SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */
SND_FILENAME = 0x00020000, /* name is file name */
SND_RESOURCE = 0x00040004 /* name is resource name or atom */
}
public static void PlayWav(string strPath)
{
PlaySound(strPath, IntPtr.Zero, PlaySoundFlags.SND_ASYNC);
}
}
更多追问追答
追问
报错了,如下所示:
Error:1 The type or namespace name 'IntPtr' could not be found (are you missing a using directive or an assembly reference?)
还有'FlagsAttribute'和'Flags'也是,我是新手,麻烦帮忙解决下,谢谢啦
追答
缺少命名空间
你加上 using System;
我就写一个System.Runtime.InteropServices; 是因为DllImport 需要这个命名空间
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询