gpt4 book ai didi

c++ - 停止后制作音频 'resume'?

转载 作者:行者123 更新时间:2023-11-28 01:46:17 28 4
gpt4 key购买 nike

我正在构建一个基于文本的 RPG,并且我已经成功地向我的程序添加了一些 .wav 文件,我也能够正常播放它们而没有问题任何一个。

目前发生了什么?

我有 2 个 .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 Game::run() 函数(在另一个 cpp 文件中)中升级时,我都会做一些增量,重置 xpCount,然后播放 levelUp。声音:

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 文件播放时简单地暂停背景歌曲,然后在播放结束后继续播放?

最佳答案

PlaySound 从来都不适合这种情况。如您所见,不支持进度通知、暂停/恢复或音量。它实际上只用于简短的通知声音。

游戏声音的替代品包括 XAudio2DirectSound .还有一些开源声音 API 也适用于您。

在更高级别,您可以使用旧版 Windows Media Player API、DirectShow 或 Media Foundation。

关于c++ - 停止后制作音频 'resume'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44960898/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com