使用 PlaySound 时听不到任何声音
我正在开发一个简单的应用程序,使用 PlaySound 播放 WAV 文件,代码如下:
#region DllImport
private enum Flags
{
SND_SYNC = 0x0000,
SND_ASYNC = 0x0001,
SND_NODEFAULT = 0x0002,
SND_MEMORY = 0x0004,
SND_LOOP = 0x0008,
SND_NOSTOP = 0x0010,
SND_NOWAIT = 0x00002000,
SND_ALIAS = 0x00010000,
SND_ALIAS_ID = 0x00110000,
SND_FILENAME = 0x00020000,
SND_RESOURCE = 0x00040004
}
[DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
private extern static int MobilePlaySound(string szSound, IntPtr hMod, int flags);
public void PlaySound(string fileName)
{
MobilePlaySound(fileName, IntPtr.Zero, (int)Flags.SND_SYNC);
}
#endregion
public Form1()
{
InitializeComponent();
openFileDialog1.ShowDialog();
PlaySound(openFileDialog1.FileName);
}
但是当我执行它并选择一个 WAV 文件(带有 2 分钟的声音)时,我没有听到任何声音。
我需要做什么?谢谢。
I'm developing a simple appplication to play WAV files using PlaySound, with this code:
#region DllImport
private enum Flags
{
SND_SYNC = 0x0000,
SND_ASYNC = 0x0001,
SND_NODEFAULT = 0x0002,
SND_MEMORY = 0x0004,
SND_LOOP = 0x0008,
SND_NOSTOP = 0x0010,
SND_NOWAIT = 0x00002000,
SND_ALIAS = 0x00010000,
SND_ALIAS_ID = 0x00110000,
SND_FILENAME = 0x00020000,
SND_RESOURCE = 0x00040004
}
[DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
private extern static int MobilePlaySound(string szSound, IntPtr hMod, int flags);
public void PlaySound(string fileName)
{
MobilePlaySound(fileName, IntPtr.Zero, (int)Flags.SND_SYNC);
}
#endregion
public Form1()
{
InitializeComponent();
openFileDialog1.ShowDialog();
PlaySound(openFileDialog1.FileName);
}
But when I execute it and select a WAV file(with 2 mins of sound) I didn't heard anything.
What I need to do? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 curtisk 指出的那样,您应该尝试使用
SND_FILENAME
让 API 知道您正在传递文件名。此外,请注意 PlaySound 方法有一个返回值,值得检查您返回的内容 - 根据 MSDN 文档,成功则返回 0,失败则返回非零。As curtisk points out, you should try using
SND_FILENAME
to let the API know you're passing in a filename. Additionally, note that the PlaySound method has a return value, it'd be worth checking what you're getting back - according to the MSDN documentation, it'll return 0 if successful and non-zero if not.您应该使用
SND_FILENAME
标志而不是SND_SYNC
,因为您正在指定一个文件,请尝试一下,看看是否可以你会有更好的结果...You should use
SND_FILENAME
flag instead ofSND_SYNC
, since you are specifying a file, try that out and see if you have better results...