Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova (Android). Disable audio play when application is minimized

When starting, the sound start, when minimized don't stop. How can I do? Through

 document.addEventListener("pause", onPause, false); 
 document.addEventListener("resume", onResume, false);

could not. Pause Event Listener could not do. Help. Sorry for bad English.

  • P.S. Requests to write a lot

var app = {
    initialize: function () {
        this.bindEvents();
    },
    bindEvents: function () {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    onDeviceReady: function () {
        app.receivedEvent('deviceready');

        var myMedia = new Media("/android_asset/www/audio/sample.mp3")
        myMedia.play({
            playAudioWhenScreenIsLocked: false
        });
    },

    receivedEvent: function () {

    }
};

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">
    <title>SOOOUND!</title>
</head>
<body>
    SOOOUND!
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
</body>

</html>
like image 348
Andrey Z. Avatar asked Jan 08 '19 23:01

Andrey Z.


2 Answers

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
}
like image 180
Kody R. Avatar answered Nov 15 '22 03:11

Kody R.


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>
like image 20
Andrey Z. Avatar answered Nov 15 '22 02:11

Andrey Z.