javascript,播放网址,onStartPlaying回调

时间:2014-11-30 10:40:52

标签: javascript audio

我播放了一些音频文件:

var audio = new Audio(audioUrl);
audio.play();

开始播放后需要发送一些ajax请求。 我如何从音频类获得回调onStartPlaying?

类似的东西:

callback = function ( ){
   ...ajax...
}
audio.onstartplaying = callback;

谢谢。

使用howler.js库。 https://github.com/goldfire/howler.js

var sound = new Howl({
  urls: ['sound.mp3', 'sound.ogg', 'sound.wav'],
  autoplay: true,
  loop: false,
  volume: 0.5,
  onplay: function() {
    console.log('On Play Callback');
  }
});

1 个答案:

答案 0 :(得分:1)

试试这个:

HEAD HTML:

<script src="http://kolber.github.io/audiojs/audiojs/audio.min.js"></script>

BODY HTML:

<span id="textbox">click the play button</span><br><br>

<audio src="http://kolber.github.io/audiojs/demos/mp3/juicy.mp3" preload="auto"></audio>

JavaScript的:

function changeText(newtext) {
    var span = document.getElementById('textbox');
    while( span.firstChild ) span.removeChild( span.firstChild );
    span.appendChild( document.createTextNode(newtext) );
}

audiojs.events.ready(function() {
    var audios = document.getElementsByTagName('audio');
    var player = audiojs.create( audios[0], {
        play: function() {
            changeText("Audio is Playing"); // change text to "Audio is Playing"
        },
        pause: function() {
            changeText("Audio is Paused"); // change text to "Audio is Paused"
        }
    });
});

JS小提琴:View Working Demo