PlaySound 在 Visual Studio 中工作,但在独立 exe 中不起作用 [英] PlaySound works in Visual Studio but not in standalone exe

查看:21
本文介绍了PlaySound 在 Visual Studio 中工作,但在独立 exe 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Visual Studio 在 C++ 中播放 wav 文件.我将文件my.wav"放在我的项目目录中并使用代码

I am trying to play a wav file in C++ using Visual Studio. I put file "my.wav" in my project directory and use the code

PlaySound(TEXT("my.wav"), NULL, SND_FILENAME | SND_SYNC);

我按下播放按钮(或 F5 甚至 Ctrl-F5),它播放的声音很好.

I hit the play button (or F5 or even Ctrl-F5) and it plays the sound fine.

我打开一个命令提示符并转到 Debug/并运行 MyApp.exe,当它运行时它会播放错误提示音.

I open a command prompt and go to Debug/ and run MyApp.exe and when it runs it plays the error chime.

注意:理想情况下,声音将捆绑在 exe 中,这样我就可以分发 exe 并且它可以工作.我尝试将它放在 Resource.rc 中,但是我在所有示例中看到的代码

Note: Ideally the sound would be bundled in the exe so I can just distribute the exe and it would work. I tried putting it in an Resource.rc but the code I see in all the examples

PlaySound( (char*)IDR_WAVE1, NULL, SND_RESOURCE | SND_SYNC );

甚至不为我编译.抱怨 IDR_WAVE1,即使这是我的资源名称.

doesn't even compile for me. Complains about IDR_WAVE1 even though that is the name of my resource.

推荐答案

我对老式 win32 有点生疏,但它是这样的:

I'm a little rusty on old school win32 but it was something like this:

在您的文件中包含 resource.h 并使用 MAKEINTRESOURCE

include resource.h in your file and use MAKEINTRESOURCE

PlaySound(MAKEINTRESOURCE(IDR_WAVE1), NULL, SND_RESOURCE | SND_SYNC );

这篇关于PlaySound 在 Visual Studio 中工作,但在独立 exe 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆