0

So when the user clicks a button an audio file plays from an array of audio files(around 2 sec clip), which works fine. However, if the user repeatedly clicks the button, the audio files start to play over each other. Ideally, I would like to stop/pause the previous audio file and then play the new audio file. This is what I've tried to avail:

$scope.sounder=function(){ $scope.rs=$scope.diff[$scope.workout_index].audiorep; $scope.ms=$scope.diff[$scope.workout_index].audiomove; //var movesound = new Audio ($scope.ms); //ar repsound = new Audio ($scope.rs); var movesound = new Media($rootScope.getMediaURL($scope.ms)); var repsound = new Media($rootScope.getMediaURL($scope.rs)); if($scope.muter==0){ movesound.pause();//DOES NOT WORK movesound.stop();//STOPS ALL AUDIO FROM PLAYING, SO NOTHING PLAYS $timeout(function() { movesound.play(); }, 1000); $timeout(function() { repsound.play(); }, 3000); } if($scope.muter==1){ console.log("Rachel has been muted"); return; } } 
2
  • Did you try movesound.destroy(); ? Commented May 29, 2017 at 20:19
  • Just tried it, doesn't allow any file to play. Commented May 30, 2017 at 3:24

1 Answer 1

1

You can achieve the functionalities with JavaScript in Cordova unless you specifically need AngularJS in your app.

<audio controls id="myAudio"> <source src="horse.ogg" type="audio/ogg"> <source src="horse.mp3" type="audio/mpeg"> Your browser does not support the HTML5 audio tag. </audio> 

Now using script to add functionalities -

<script> var aud= document.getElementById("myAudio"); function playAud() { aud.play(); } function pauseAud() { aud.pause(); } function myFunction() { isSupp = aud.canPlayType("audio/mp3"); if (isSupp == "") { aud.src = "audio.ogg"; } else { aud.src = "audio.mp3"; } aud.load(); } </script> 

See other answers of this question on for changing source of audio with JavaScript.

Refer w3schools HTML Audio Video DOM reference Page for further attributes and functions.

Sign up to request clarification or add additional context in comments.

1 Comment

I appreciate this answer, but yes it has to be AngularJS as there are other factors in which it is useful for my app. Thanks anyway :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.