我正在构建一个基于文本的RPG,并且我已经成功地将一些.wav文件添加到我的程序中,没有问题,我也能够在没有问题的情况下正确地播放它们。
当前发生了什么?
我有两个.wav文件,一个用于一般背景音乐(newbieMelody.wav),另一个用于当您升级时(levelUp.wav)。因此,首先,当我运行我的游戏时,我们从后台播放的newbieMelody.wav文件开始,如下所示:
#pragma comment(lib, "winmm.lib")
#include "Quiz.h"
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
int main() {
PlaySound(TEXT("C:\\Users\\User\\Documents\\GIT\\TextBased\\QuizApplication\\playSound\\newbieMelody.wav"),
NULL, SND_FILENAME | SND_ASYNC | SND_LOOP); // we play the sound here
// create a game
Game *game = new Game();
game->readAreaFromFile(); // calls a file for area location
game->run(); // loads the game UI
//destroy gameWorld
delete game;
system("PAUSE");
}然后,每当我们在void::run()函数(在另一个cpp文件中)中升级时,我会执行一些增量、重置xpCount和播放levelUp.wav。
if (xpCount >= xpPeak) { // if current exp is larger or = to xp cap
combatLevel += 1; // current combat lvl
xpCount = 0; // records total exp
xpPeak += 4; // takes longer to level up each level
setcolor(yellow, black); // sets location green
cout << "Congratulations, you just advanced your Combat level!" << endl;
cout << "Your Combat level is now " << combatLevel << "." << '\n' << endl;
PlaySound(TEXT("C:\\Users\\User\\Documents\\GIT\\TextBased\\QuizApplication\\playSound\\levelUp.wav"),
NULL, SND_FILENAME | SND_ASYNC); // play level up sound
this_thread::sleep_for(chrono::seconds(5)); // sleeps output to allow level up song to finish
levelUpDone = true; // leveled up
}一旦播放完毕,我们将“继续”播放newbieMelody.wav文件:
if (levelUpDone == true) { // access since player leveled up
PlaySound(TEXT("C:\\Users\\User\\Documents\\GIT\\TextBased\\QuizApplication\\playSound\\newbieMelody.wav"),
NULL, SND_FILENAME | SND_ASYNC | SND_LOOP); // resume newbie melody???
levelUpDone = false; // back to false to avoid errors
},怎么回事?
这通常是我所希望的工作方式,但是当我的newbieMelody.wav文件在levelUp.wav文件之后再次播放时,从一开始就重新启动,而不是从它上次播放的位置恢复。
我的问题是,我如何能够修改我的代码,使背景音乐从它上次播放的位置恢复,而不是重新开始呢?有什么方法,我可以简单地暂停背景歌曲,而levelUp.wav文件播放,然后恢复,一旦它完成?
发布于 2017-07-07 01:03:22
PlaySound从来就不是针对这种场景的。正如您所观察到的,不支持进度通知、暂停/恢复或卷。它实际上只适用于简短的通知声音。
游戏声音的替代品包括XAudio2和DirectSound。还有一些开源的声音API也适用于您。
在更高的级别上,您可以使用遗留的、DirectShow或。
https://stackoverflow.com/questions/44960898
复制相似问题