Search code examples
cordovamediacordova-plugins

Stop All Audio Cordova Media Plugin?


Is there a way to stop ALL audio playing within Cordova Media? Here is my code inside of an angular controller:

function soundCtrl($scope) {
    $scope.playSound = function(file) {
        myMedia.stop();
        var myMedia = new Media(file)
        myMedia.play({
            playAudioWhenScreenIsLocked: true
        })
    }

Solution

  • you can keep all myMedia objects in an array, when you want to stop all of them just read array and stop them one by one.

    function soundCtrl($scope) {
        var myMedias = [];
        $scope.playSound = function(file) {
            var myMedia = new Media(file);
            myMedias.push(myMedia);
            myMedia.play({
                playAudioWhenScreenIsLocked: true
            })
        }
        $scope.stopAllSounds = function() {
            for(int i=0; i < myMedias.length; i++){
                myMedias[i].stop();
            }
        }