【问题标题】:Make audio 'resume' after being stopped?停止后让音频“恢复”?
【发布时间】:2017-07-07 00:48:50
【问题描述】:

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

目前发生了什么?

我有 2 个 .wav 文件,1 个用于一般背景音乐 (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
        }

播放完毕后,我们“继续”播放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 文件之后再次播放时,它会从头开始重新启动,而不是恢复 strong> 从上次播放的地方开始。

我的问题是,我究竟如何能够更改我的代码,以便背景音乐从上次播放的位置继续播放,而不是重新开始?有什么方法可以让我在levelUp.wav 文件播放时简单地暂停背景歌曲,然后在播放完成后继续播放?

【问题讨论】:

  • 如果您要等待声音结束,为什么要异步播放并休眠? PlaySound 完全有能力阻止,直到它完成。如果您想继续学习,您将需要更多地了解 Windows 中的多媒体,PlaySound 是您可以做的最基本的事情。
  • 您对 SND_ASYNC 的看法是正确的,我现在已经对其进行了排序,谢谢。

标签: c++ playsound


【解决方案1】:

PlaySound 从来不适合这种类型的场景。正如您所观察到的,不支持进度通知、暂停/恢复或音量。它实际上只适用于简短的通知声音。

游戏声音的替代品包括XAudio2DirectSound。还有一些开源声音 API 也可以为您工作。

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

【讨论】:

  • 我想通了,谢谢 Selbie。我会自己寻找更好的选择。
猜你喜欢
  • 1970-01-01
  • 2012-08-26
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多