停止后制作音频'resume'?

Make audio 'resume' after being stopped?

我正在构建一个基于文本的角色扮演游戏,我已经成功地向我的程序添加了一些 .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.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
        }

播放完毕后,我们 "resume" 播放 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 从来都不适合这种情况。正如您所观察到的,不支持进度通知、pause/resume 或音量。它实际上只用于简短的通知声音。

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

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