14

Using an API to obtain the url of the sound to play in audio tag
View

<img src="../img/play.svg" alt="Play" ng-click="playSound('exampleWord')" width="48"/>

Controller:

$scope.playSound=function(input){
    $scope.audio={};
    soundFetch.getSound(input).success(function(data){
        $scope.audio=data;
    });
}

The soundFetch is the service to call the getSound function. The returning data is the url of the song. How can I play the sound after clicking the img tag. Currently getting an error Error: [$interpolate:interr]

LOTUSMS
  • 10,317
  • 15
  • 71
  • 140
Akansh
  • 1,715
  • 3
  • 15
  • 34

4 Answers4

32
    $scope.playAudio = function() {
        var audio = new Audio('audio/song.mp3');
        audio.play();
    };
Rejneesh
  • 1,574
  • 16
  • 16
1
var playSound = function(mp3File){
  var audioElement = document.createElement('audio');
  audioElement.setAttribute('src', "/sound/"+mp3File);
  audioElement.play();
}
user3871424
  • 244
  • 4
  • 4
0

It seems playSound function takes input as argument

I'd try

ng-click="playSound(this)" or in the case of button

ng-click="playSound(document.getElementById('audio'))"

Jacek Pietal
  • 1,980
  • 1
  • 18
  • 27
0
  var audioFile = new Audio($scope.audioUrl); 
            audioFile.play();  //we can not get metadata until audio is fetched
            audioFile.pause(); //as soon as audio playes it pause so user does not hear a sound 

 audioFile.onplay = function () {
                audioFile.preload("metadata");         
            };

            audioFile.onloadedmetadata = function () {
                console.log(audioFile.duration);
                $scope.audioDuration = audioFile.duration;
            };

when audio play method is called the metadata are loaded

Hiren More
  • 23
  • 2
  • 8