在 C++/OpenGL 中使用 PlaySound() 在后台播放声音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8804880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:08:19  来源:igfitidea点击:

use PlaySound() in C++/OpenGL to play sound in background

c++cwindowsaudiographics

提问by biggdman

I am trying to play a wav file in the background of a game built in c++ with opengl. I am using the following line to play the wav file:

我正在尝试在使用 opengl 用 C++ 构建的游戏的背景中播放 wav 文件。我正在使用以下行播放 wav 文件:

 PlaySound("starwars.wav", NULL, SND_FILENAME|SND_LOOP);

The problem is when the music starts the animation stops. I tried starting the music at the press of a keyboard button, but when I do that the music starts and the all the animation stops. Is there a way to avoid this? I just want some music to play in the background and PlaySound seemed the simplest way to achieve that, given the fact it requires just a line of code.

问题是当音乐开始时动画停止。我尝试通过按下键盘按钮开始播放音乐,但是当我这样做时,音乐开始并且所有动画都停止了。有没有办法避免这种情况?我只想在后台播放一些音乐,而 PlaySound 似乎是实现这一目标的最简单方法,因为它只需要一行代码。

回答by Carl Winder

You want to pass in

你想传入

SND_ASYNC

SND_ASYNC

This would make PlaySound return immediately, rather than waiting for the sound to finish playing, which in your case wouldn't as you are looping. IIRC PlaySound only allows one sound to play at any one time so it may be best to look for a sound library, especially if you are making a game.

这将使 PlaySound 立即返回,而不是等待声音完成播放,这在您的情况下不会在您循环播放时完成。IIRC PlaySound 只允许在任何时间播放一种声音,因此最好寻找声音库,尤其是在您制作游戏时。

In conclusion for your sample to work:

总之,您的样本可以正常工作:

PlaySound("starwars.wav", NULL, SND_ASYNC|SND_FILENAME|SND_LOOP);

PlaySound("starwars.wav", NULL, SND_ASYNC|SND_FILENAME|SND_LOOP);

Please see this

请看这个

回答by Constantinius

I'm no expert with the windows sound API, but it seems like the function PlaySoundis a blocking operation, which means your application freezes until the function is finished, i.e the sound is played until the end. Maybe there is a flag to circumvent this?

我不是 Windows 声音 API 的专家,但似乎该功能PlaySound是一个阻塞操作,这意味着您的应用程序会冻结,直到该功能完成,即声音一直播放到结束。也许有一个标志来规避这个?

You could start a thread for the sound, which finishes after the sound has stopped. Or you might want to look into a simple sound API.

您可以为声音启动一个线程,该线程在声音停止后结束。或者您可能想研究一个简单的声音 API。

回答by yemane

If you are using the format playSound("*.wav",NULL,SND_SYNC|SND_LOOP) almost you are out of control of the animation of the game which will be frozen with the loop included in the playSound() function which is SND_LOOP but if you change the SND_SYNC with SND_ASYNC it would work exactly as you ordered but do not forget that this works for windows and do not forget to include the WINMM.LIB (window multimedia library) under the project/opengl/visual c++/link

如果您使用格式 playSound("*.wav",NULL,SND_SYNC|SND_LOOP) 几乎您将无法控制游戏的动画,该动画将被包含在 playSound() 函数中的循环冻结,该函数是 SND_LOOP 但如果您使用 SND_ASYNC 更改 SND_SYNC,它将完全按照您的命令工作,但不要忘记这适用于 Windows 并且不要忘记在 project/opengl/visual c++/link 下包含 WINMM.LIB(窗口多媒体库)