如何在angularjs中播放声音

时间:2015-06-17 02:01:05

标签: angularjs audio

使用API​​获取要在音频标签中播放的声音的网址
查看

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

控制器:

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

soundFetch是调用getSound函数的服务。返回的数据是歌曲的网址。 单击img标签后如何播放声音。目前收到错误Error: [$interpolate:interr]

3 个答案:

答案 0 :(得分:28)

    $scope.playAudio = function() {
        var audio = new Audio('audio/song.mp3');
        audio.play();
    };

答案 1 :(得分:1)

var playSound = function(mp3File){
  var audioElement = document.createElement('audio');
  audioElement.setAttribute('src', "/sound/"+mp3File);
  audioElement.play();
}

答案 2 :(得分: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;
            };

当调用音频播放方法的元数据被加载时