Cordova (Android). Disable audio play when application is minimized
You'll have to add a Pause Event Listener to your application to listen for whenever it goes into the background.
document.addEventListener("onDeviceReady", function() {
// Adds pause event
document.addEventListener("pause", manageAppPause);
// Adds resume event
document.addEventListener("resume", manageAppResume);
});
function manageAppPause() {
// Check if audio is playing
// If it is, pause it
}
function manageAppResume() {
// If you want, check if the audio was playing before the app was put into background
// If so, resume audio
}
var app = {
initialize: function () {
this.bindEvents();
},
bindEvents: function () {
document.addEventListener('deviceready', this.onDeviceReady, false);
},
onDeviceReady: function () {
var myMedia = new Media("/android_asset/www/audio/sample.mp3")
myMedia.play({
playAudioWhenScreenIsLocked: false
});
document.addEventListener('pause', onPause, false);
document.addEventListener('resume', onResume, false);
function onPause() {
myMedia.pause();
};
function onResume() {
myMedia.play();
};
},
};
app.initialize();
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<title>SOOOUND!</title>
</head>
<body>
SOOOUND!
</body>
</html>