简体   繁体   中英

How to stop Audio when App closes Android

In iOS this is stadard. But on Android when I play aution in the app and close the app the audio still plays. How can I stop this?

This is what I have:

vid = document.getElementById("audio2");

$scope.$on('$ionicView.beforeLeave', function(){
    vid.pause();
    vid.currentTime = 0;
});

$scope.$on('$ionicView.afterEnter', function(){
    vid.pause();
    vid.currentTime = 0;
});


vid.onended = function() {
    $timeout(function() {
      $scope.pause = true;
    }, 0);
}

$scope.pause = true;

$scope.playAudio = function() {
    vid.play();
    $scope.pause = false;
}

$scope.pauseAudio = function() {
    vid.pause();
    $scope.pause = true;
}

It depends on how you want your app to behave but you could use cordova's resume and pause events to stop audio when leaving/closing your app and starting it when entering your app again:

function onLoad() {
    document.addEventListener("deviceready", onDeviceReady, false);
}

function onDeviceReady() {
    document.addEventListener("resume", onResume, false);
    document.addEventListener("pause", onPause, false);
}

// called when entering your app
function onResume() {
   var scope = angular.element(document.getElementById('your view element')).scope();
   scope .playAudio();
}

// called when leaving your app 
function onPause() {
   var scope = angular.element(document.getElementById('your view element')).scope();
   scope.pauseAudio();
}

For more info: resume and pause .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM