Unfocused game window + audio pausing

ZirconStorms

VX & VX Ace Scripts
Veteran
Joined
Dec 22, 2014
Messages
342
Reaction score
110
First Language
English
Primarily Uses
RMVXA
Once you click away from the game or minimize it, the audio in-game still plays in the background. Is there a way to either silence the audio or completely pause it until the player clicks back into the game or focuses on that window?
There's a few threads floating around about solutions for Ace, so I'm wondering if there's anything similar for MV.
 

Yethwhinger

Veteran
Veteran
Joined
Jul 18, 2018
Messages
53
Reaction score
27
First Language
English
Primarily Uses
RMMV
Feel free to try the plugin below. It is supposed to pause the whole game and silence the audio when the game window loses focus. Audio and the rest of the game resume when the window regains focus.
Code:
// PauseOnBlur.js
// Created on 9/27/2018

var objYeth = objYeth || {};

/*:
* @plugindesc This plugin is meant to pause the game
* when the game window loses focus.
* @author Yethwhinger
*
* @help This plugin saves the BGM and BGS that are playing
* when the game window loses focus and stops them. It also
* stops updating the game. It resumes updates and replays
* the BGM and BGS when the window regains focus.
*/

objYeth.pauseAudio = function () {
    objYeth._pausedBgm = AudioManager.saveBgm();
    objYeth._pausedBgs = AudioManager.saveBgs();
    AudioManager.stopAll();
};

objYeth.resumeAudio = function () {
    if (objYeth._pausedBgm) {
        AudioManager.replayBgm(objYeth._pausedBgm);
    }
    if (objYeth._pausedBgs) {
        AudioManager.replayBgs(objYeth._pausedBgs);
    }
};

objYeth.pauseGame = function () {
    objYeth.pauseAudio();
    objYeth._gamePaused = true;
};

objYeth.resumeGame = function () {
    objYeth.resumeAudio();
    objYeth._gamePaused = false;
    SceneManager.update();
};

objYeth._gamePaused = false;
window.onblur = objYeth.pauseGame;
window.onfocus = objYeth.resumeGame;

//----------------------------
// Changes to SceneManager
//----------------------------

objYeth.SceneManager_update = SceneManager.update;
SceneManager.update = function () {
    if (!objYeth._gamePaused) {
        objYeth.SceneManager_update.call(this);
    }
};
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Stream will be going live early today as I will be working on some music before I transition to art! When I switch to art, I will be focusing on making a new stream avatar~ Feel free to drop by!
For anyone who has uploaded a game to Steam and wonders if they actually check your game's build when you first upload it, I can personally vouch for Steam.
6 more towns to make in my game. SIX. not done with interiors yet but SIX EXTERIORS.
what to do when you come across a person that has stated "What if I say, f*** their EULA? I could probably get away with it." concerning asset packs sold here... on this site ...
So a guy enters in a bar and walks up to the counter. He looks the bartender with a mysterious look and asks him...

Forum statistics

Threads
93,496
Messages
912,996
Members
123,031
Latest member
zojin
Top