mediaelementjs event pause after playing new stream

47 views Asked by At

I use last version of mediaelementjs (v7) and it works well but I have just a little problem, I use this script:

    $(document).on('click', '.header a[rel=open_player]', function() {
        var bottom_player = $(document).find('.bottom-player');
        var webradio_id = $(this).attr('data-id');
        isPlaying = true;
        var currentStream;

        $.post('/ajax/webradio/'+ webradio_id, function(data) {
            bottom_player.html(data);
            var player = bottom_player.find('audio');
            
            player.mediaelementplayer({
                audioWidth: '100%',
                pluginPath: '/assets/public/js/mediaelement/',
                alwaysShowControls: true,
                features: ['playpause','volume'],
                audioVolume: 'horizontal',
                isVideo: false,
                forceLive: true,
                success: function (mediaElement, domObject) {
                    mediaElement.play();
                    currentStream = mediaElement.getSrc();
                    mediaElement.addEventListener('play', function () {
                        console.log('['+Date.now() + '] Media play: getSrc='+ mediaElement.getSrc()+' / currentStream='+currentStream);
                    document.title = '\u25B6 ' + origTitle;
                    isPlaying = true;
                }, false);
                mediaElement.addEventListener('pause', function () {
                    if(currentStream == mediaElement.getSrc()) {
                         console.log('['+Date.now() + '] Media pause: getSrc='+ mediaElement.getSrc() + ' / currentStream='+currentStream);  // Log de l'état du listener
                         document.title = origTitle;
                         isPlaying = false;
                     }
                }, false);
            },
        });
    });

My problem is when I play a stream, in the console I have: "Media play", and when I click on another stream to play it instead of the first, in the console I have: "Media play" "Media pause" but it plays well the new stream. My problem is that I have isPlaying = false because "pause" Event is fired after play.

Console logs:

[1695287178446] Media play: getSrc=https://example.com/stream-wr03.aac?i=94884 / currentStream=https://example.com/stream-wr03.aac?i=94884
[1695287178449] Media pause: getSrc=https://example.com/stream-01.aac?i=30130 / currentStream=https://example.com/stream-01.aac?i=30130
[1695287190405] Media play: getSrc=https://example.com/stream-wr01.aac?i=24717 / currentStream=https://example.com/stream-wr01.aac?i=24717
[1695287190409] Media pause: getSrc=https://example.com/stream-wr03.aac?i=94884 / currentStream=https://example.com/stream-wr03.aac?i=94884

Any idea to avoid that?

0

There are 0 answers