javascript - HTML5 - how to stop audio when another audio tag starts playing? - Stack Overflow

I have this javascript function:var currentPlayer;function EvalSound(soundobj) {var thissound = docum

I have this javascript function:

var currentPlayer;
function EvalSound(soundobj) {
    var thissound = document.getElementById(soundobj);
    if (currentPlayer && currentPlayer != thissound) {
        currentPlayer.pause(); 
    }
    if (thissound.paused) {
        thissound.play();
    } else {
        thissound.pause();
    }
    thissound.currentTime = 0;
    currentPlayer = thissound;
}

And this html5 audio player repeating in a loop inside a Gridview widget in Yii2 (it creates many players each of them with a different song):

'value'=> function ($data){                      
    return "<audio controls>
            <source src='" . $data->ficheiro . "' type='audio/mp3' />
            </audio>";
},

The problem is when I have an audio tag playing and click on the play button of another it doesn't stop the previous playing audio tag and starts the new one. Both audio tags play at the same time. I'm trying to adapt the javascript function but it doesn´t work.

I also tried declaring var thisound = $('audio');

It also does not work.

Do I need to put an ID in the audio tag? Do I need to associate an onClick='EvalSound' event to the audio tag?

I have this javascript function:

var currentPlayer;
function EvalSound(soundobj) {
    var thissound = document.getElementById(soundobj);
    if (currentPlayer && currentPlayer != thissound) {
        currentPlayer.pause(); 
    }
    if (thissound.paused) {
        thissound.play();
    } else {
        thissound.pause();
    }
    thissound.currentTime = 0;
    currentPlayer = thissound;
}

And this html5 audio player repeating in a loop inside a Gridview widget in Yii2 (it creates many players each of them with a different song):

'value'=> function ($data){                      
    return "<audio controls>
            <source src='" . $data->ficheiro . "' type='audio/mp3' />
            </audio>";
},

The problem is when I have an audio tag playing and click on the play button of another it doesn't stop the previous playing audio tag and starts the new one. Both audio tags play at the same time. I'm trying to adapt the javascript function but it doesn´t work.

I also tried declaring var thisound = $('audio');

It also does not work.

Do I need to put an ID in the audio tag? Do I need to associate an onClick='EvalSound' event to the audio tag?

Share Improve this question edited Jan 4, 2022 at 6:22 HoRn 1,5185 gold badges20 silver badges28 bronze badges asked Aug 13, 2015 at 9:01 André CastroAndré Castro 1,5656 gold badges34 silver badges62 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Ok, here is how I solved it:

The addEventListener grabs the audio beginning to play and the for loop pauses all other audio tags.

document.addEventListener('play', function(e) {
    var audios = document.getElementsByTagName('audio');

    for (var i = 0, len = audios.length; i < len; i++) {
        if (audios[i] != e.target) {
            audios[i].pause();
        }
    }
}, true);

You don't have a unique way of identifying the <audio> tags. And using a generic $("audio") seelctor makes the issue worse. Use id for all the tags and make it work that way.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745156480a4614152.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信