C++ 控制台应用程序中的 PlaySound?

PlaySound in C++ Console application?(C++ 控制台应用程序中的 PlaySound?)

本文介绍了C++ 控制台应用程序中的 PlaySound?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已编辑,因此代码正确(感谢 ta.speot.is) - 底部的新问题

EDITED SO THE CODE IS CORRECT (THANKS TO ta.speot.is) - NEW QUESTION AT BOTTOM

所以我一直在玩这个级别的控制台,我们被要求制作我们的第一个项目".进行评估.我已经完成了基本的应用程序.. 但我想稍微提高一下并添加一些声音.将从控制台播放的声音.

So I have been playing with the console as I am at that level, and we have been asked to make our first "project" for a assessment. I have the basic application all done.. but i wanted to jaz it up a bit and add some sounds. Sounds that will play from the console.

这个测试有效(有点),因为它会播放一个声音文件,但有两个问题......

This test works(kind of), as it will play a sound file, but there are 2 problems...

  1. 当声音开始播放时,应用程序会冻结直到播放结束.

  1. When the sound starts playing, the application freezes until it finishes playing.

当我尝试编译为发布"时它因链接错误"而出错;- 致命错误 LNK1120:1 个未解决的外部问题.

When I try to compile as a "release" it errors with a "linking error" - fatal error LNK1120: 1 unresolved externals.

#include <iostream>
#include <windows.h>
#include <mmsystem.h>
using namespace std;

int main(){
    //PlaySound(TEXT("mywavsound.wav"), NULL, SND_FILENAME); - My erroring code
    PlaySound(TEXT("mywavsound.wav"), NULL, SND_FILENAME | SND_ASYNC);// - the correct code
        
    int test = 0;
    cin>>test;
    return 0;
}

所以我的问题是......

So my questions are...

  1. 如何在不冻结控制台的情况下播放声音,这样我就可以示例在项目中播放循环音乐文件在跑?如果我能在上面播放其他声音也很棒其中,例如,当您按下回车键时,它会播放声音不停止音乐.

如何添加 wav 文件以便将其编译为发行版?

编辑

我知道 SND_ASYNC 的东西,但我不知道如何使用它,如果没有不编译的东西,我似乎无法使用它.. 有没有人有使用代码的示例SND_ASYNC?

I know about the SND_ASYNC thing but I do not know how to use it, I can't seem to use it without the thing not compiling.. does anyone have an example of a code using SND_ASYNC?

编辑 2

所以我现在有这个工作......使用

So I have this working now.... using the

PlaySound(TEXT("mysound.wav"), NULL, SND_FILENAME | SND_ASYNC);

现在我想知道如何一次播放 1 个或多个声音,因为如果我使用该标志调用 PlaySound() 两次,它将停止第一个并播放第二个..有没有办法同时播放 2 个声音?

Now I am wondering how I can get 1 or more sounds to play at once, for if I call PlaySound() twice with that flag it will stop the 1st one and play the 2nd.. Is there a way to play 2 sounds at once?

推荐答案

如何在不冻结控制台的情况下播放声音?

How can I play sounds without freezing the console?

如果您使用 Google 搜索 PlaySound 这是第一个结果:

If you Google for PlaySound this is the first result:

...

SND_ASYNC 声音异步播放,PlaySound 在声音开始后立即返回.要终止异步播放的波形声音,请调用 PlaySound 并将 pszSound 设置为 NULL.

SND_ASYNC The sound is played asynchronously and PlaySound returns immediately after beginning the sound. To terminate an asynchronously played waveform sound, call PlaySound with pszSound set to NULL.

您应该熟悉搜索引擎及其功能.

You should familiarise yourself with search engines and what they are capable of.

如何添加 wav 文件以便将其编译为发行版?

How do I add the wav file so it compiles as a release?

您需要在 Release 和 Debug 配置中链接 winmm.lib.或者,添加

You need to link winmm.lib in both Release and Debug configurations. Alternatively, add

#pragma comment(lib, "winmm.lib")

到源代码的顶部.

这篇关于C++ 控制台应用程序中的 PlaySound?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:C++ 控制台应用程序中的 PlaySound?

基础教程推荐