1

在 C++ 中播放波形文件时,程序不再执行任何其他操作。通常,您必须等待曲目完成然后程序才能继续,但我正在播放循环曲目,我需要它在程序执行时播放。有没有办法做到这一点?谢谢。

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

cout << "x1" << endl;
cin >> x1;

cout << "y1" << endl;
cin >> y1;

cout << "x2" << endl;
cin >> x2;

cout << "y2" << endl;
cin >> y2;

double f = slope (x1,y1,x2,y2);

cout << "y = " << m << "x + " << yi << endl;
4

2 回答 2

3

根据文档

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.

所以:

PlaySound("sleep_away.wav", NULL, SND_FILENAME|SND_LOOP|SND_ASYNC);
于 2013-11-10T21:39:27.713 回答
1

正如 Benjamin Lindley 所写,API 中有 SND_ASYNC 选项,我没有考虑过。或者,您可以享受线程的乐趣。

或者

如果您想在执行其他代码的同时继续播放音乐,您需要在不同的线程中启动音乐播放代码。您可以使用C++11线程来完成它。

示例代码

#include <iostream>
#include <thread>

void play_music() {
  PlaySound("sleep_away.wav", NULL, SND_FILENAME|SND_LOOP);
}

int main(int argc, char* argv[])
{
  std::thread t(play_music);

  // other code

  t.join();

    return 0;
}
于 2013-11-10T21:35:04.353 回答