Make audio 'resume' after being stopped?

人走茶凉 提交于 2019-12-12 04:58:42

问题


I'm building a Text-Based RPG, and I've managed to add some .wav files to my program with no issue, I'm also able to play them properly with no issue either.

What happens currently?

I have 2 .wav files, 1 for general background music (newbieMelody.wav), and the other for when you level up (levelUp.wav). So to start off, when I run my game, we begin with the newbieMelody.wav file to play in the background, like so:

#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");
}

Then, whenever we level up in my void Game::run() function (in another cpp file), I do a few increments, reset xpCount, and play the 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
        }

Once this has finished playing, we "resume" with playing the newbieMelody.wav file:

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
        }

What's the issue?

This generally works how I'd like it to, however when my newbieMelody.wav file plays again after the levelUp.wav file, it restarts from the beginning instead of resuming from where it was last played.

My question is, how exactly am I able to alter my code so the background music resumes from where it was last playing from, rather than starting over again? Is there some way I can simply pause the background song whilst the levelUp.wav file plays, then resume once it's finished?


回答1:


PlaySound was never meant for this type of scenario. As you have observed, there is no support for progress notifications, pause/resume, or volume. It was really only meant for short notification sounds.

Alternatives for game sounds include XAudio2 and DirectSound. There's also some open source sound APIs that could work for you too.

At a higher level, you could use legacy Windows Media Player APIs, DirectShow, or Media Foundation.



来源:https://stackoverflow.com/questions/44960898/make-audio-resume-after-being-stopped

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!