weixin_33713350 2014-10-01 02:33 采纳率: 0%
浏览 58

播放音乐javascript

My code is below. Basically, my goal is once the user clicks certain button, I would get this button's id and send it through ajax, then return the mp3 url based on the id specified and play the sound. Everything is working perfectly fine, but the problem is I do not know how to stop or pause the music. I've tried audio.stop(), or audio.pause(). Neither one is working.

Once the user clicks certain button, i would like to stop all the music and play the new one that is clicked.

$('.play_sound').click(function(event){
event.preventDefault();
var data_id = $(this).parents('tr').attr('data-id');

  $.post('ajax/play_sound.php', {data_id: data_id}, function(data){
     var audio = new Audio(data);
     audio.play();
  });

});
  • 写回答

2条回答 默认 最新

  • weixin_33698043 2014-10-01 03:30
    关注

    you should create a audio tag

    <audio src="audio.mp3" id="audio"></audio>
    

    then

    document.getElementById('audio').pause();
    
    $.post('ajax/play_sound.php', {data_id: data_id}, function(data){
        document.getElementById('audio').src = data;
        document.getElementById('audio').play();
    });
    

    Haven't tested, hope it woks.

    评论

报告相同问题?