Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setSrc() strange behaviour #1914

Closed
NitroxydeX opened this issue Nov 3, 2016 · 29 comments
Closed

setSrc() strange behaviour #1914

NitroxydeX opened this issue Nov 3, 2016 · 29 comments

Comments

@NitroxydeX
Copy link

NitroxydeX commented Nov 3, 2016

window.onload = function() { myaudio = document.getElementById('audio-player'); button = $('.mejs-playpause-button'); i = Math.floor( ( Math.random() * 1000000 ) + 1); source = 'http://stream.url:PORT;'; playpause = document.querySelector('.mejs-playpause-button'); playpause.addEventListener('click', function() { if (myaudio.paused) { myaudio.setSrc(''); myaudio.load(); myaudio.pause(); button.removeClass('mejs-pause').addClass('mejs-play'); } else { i++; myaudio.setSrc(source + '?nocache=' + i); myaudio.load(); myaudio.play(); button.removeClass('mejs-play').addClass('mejs-pause'); } }); };

I noticed some strange behavior across all browsers. The Code shows my way I tricked the audio player to force a STOP - since HTML5 audio isn't able to do this. What do I need this for? Audio-Livestreams. Otherwise the Browser would save all Audio into the RAM, which is bad and not live anymore. lul.

But this isn't working anymore (last time it worked was 4 months ago). It just can't set the src on the audio tag - it stays empty after clearing it with the pause button.

But if I change if (myaudio.paused) { myaudio.setSrc(''); to if (myaudio.paused) {i++; myaudio.setSrc(source + '?nocache=' + i); it changes the URL again in my else part. This is really weird. Hope I just do bad things now that it's not working...

@rafa8626
Copy link
Contributor

rafa8626 commented Nov 3, 2016

Can you provide a URL where you show this issue, please? The only way I knew to stop an audio streaming on html5 is by setting the source empty and then destroying the audio tag

@NitroxydeX
Copy link
Author

You could look here: https://thgfm.de/socialradio/
The stream isn't working on this player, but this doesn't change the fact, that setSrc is not working. I always just cleared it by setSrc(''); and readded the stream later with setSrc. But audio-tag seems to refuse to do this action

@rafa8626
Copy link
Contributor

rafa8626 commented Nov 4, 2016

@NitroxydeX Ok I'll check this and let you know my findings. Thanks

@rafa8626
Copy link
Contributor

rafa8626 commented Nov 4, 2016

@NitroxydeX Can you test the 3.x-dev branch and let me know if that's an issue there?

Use also the following code as a replacement

$(window).load(function() {

            var
                    target = $('#player2').closest('.mejs-container'),
                    playerId = target.attr('id'),
                    player = mejs.players[playerId],
                    button = target.find('.mejs-playpause-button'),
                    i = Math.floor( ( Math.random() * 1000000 ) + 1),
                    source = '[YOUR SOURCE]'
            ;

            button.on('click', function(e) {
                if (player.media.paused) {
                    console.log('PAUSED');
                    player.setSrc('');
                    player.load();
                    player.pause();
                } else {
                    console.log('RESTORED');
                    i++;
                    player.setSrc(source + '?nocache=' + i);
                    player.load();
                    player.play();
                }

                return false;
            });

        });

Let me know if it worked for you. Thanks

@NitroxydeX
Copy link
Author

NitroxydeX commented Nov 4, 2016

Well it's not working in general now... The 3.x branch is also missing silverlight. Is this correct? Console also said that the code is wrong. Doesn't work like this in jQuery 3 anymore. Changed back to jQuery 2 still not working. Changed the target div-id from #player2 to #audio-player but same result. It doesn't start.

Take a look here: https://thgfm.de/test

@rafa8626
Copy link
Contributor

rafa8626 commented Nov 4, 2016

Silverlight is now deprecated. I'll take a look on your code

@rafa8626
Copy link
Contributor

rafa8626 commented Nov 4, 2016

@NitroxydeX I know why it has to do with the src being in the audio tag and not in the source tag. I'm gonna check this but test your code with something like this and it should work

<audio autoplay>
    <source src="http://thgfm.de:8000/dsl;" type="audio/mp3">
</audio>

@rafa8626
Copy link
Contributor

rafa8626 commented Nov 4, 2016

@NitroxydeX Please download the latest version; it has the fix for this situation. Thanks for pointing it out

@NitroxydeX
Copy link
Author

Yeah, thats working now. We're getting closer to a solution. It removes the Audio src now on "pause" but it seems that it doesn't load with the empty source. If you hit play again it starts playing but not the live feed instead it takes the buffered feed that still got saved and no new src is set.

@rafa8626
Copy link
Contributor

rafa8626 commented Nov 4, 2016

Gotcha checking it right now

@rafa8626
Copy link
Contributor

rafa8626 commented Nov 7, 2016

@NitroxydeX After a lot of research I think I finally figured out. Download the latest version and use this code to stop/resume the streaming:

$(window).load(function() {

        var
                target = $('#player2').closest('.mejs-container'),
                playerId = target.attr('id'),
                player = mejs.players[playerId],
                button = target.find('.mejs-playpause-button'),
                i = Math.floor( ( Math.random() * 1000000 ) + 1),
                source = 'http://thgfm.de:8000/dsl;',
                first = true;
            ;

        button.on('click', function(e) {

            if (player.media.readyState > 0) {
                player.pause();
                player.setSrc('');
                player.load();
                button.removeClass('mejs-play').addClass('mejs-pause');
                first = false;
            } else {
                i++;
                player.setSrc(source + '?nocache=' + i);
                player.load();
                player.play();
                button.removeClass('mejs-pause').addClass('mejs-play');
            }

            return false;
 });

Let me know if it works for you

@rafa8626
Copy link
Contributor

rafa8626 commented Nov 7, 2016

If this works as you expected, I'll modify the current stop feature to account for this workflow. Thanks for your help on this one and keep me posted. This was truly a tricky one

@NitroxydeX
Copy link
Author

difficult to test. every classes have wrong names in the latest release. So instead of 'mejs-container' it's 'mejs__container'. I think this is a fail.

@rafa8626
Copy link
Contributor

rafa8626 commented Nov 7, 2016

no it's a new element in the 3.0 version using BEM naming convention. My bad I merged today and I forgot to update the code above

@rafa8626
Copy link
Contributor

rafa8626 commented Nov 7, 2016

$(window).load(function() {

        var
                target = $('#player2').closest('.mejs__container'),
                playerId = target.attr('id'),
                player = mejs.players[playerId],
                button = target.find('.mejs__playpause-button'),
                i = Math.floor( ( Math.random() * 1000000 ) + 1),
                source = 'http://thgfm.de:8000/dsl;',
                first = true;
            ;

        button.on('click', function(e) {

            if (player.media.readyState > 0) {
                player.pause();
                player.setSrc('');
                player.load();
                button.removeClass('mejs__play').addClass('mejs__pause');
                first = false;
            } else {
                i++;
                player.setSrc(source + '?nocache=' + i);
                player.load();
                player.play();
                button.removeClass('mejs__pause').addClass('mejs__play');
            }

            return false;
 });

@NitroxydeX
Copy link
Author

ah well okay. I need to update my whole CSS as well then. will come back if i did this.

@rafa8626
Copy link
Contributor

rafa8626 commented Nov 7, 2016

FYI, I just posted the updated code hope it helps

@NitroxydeX
Copy link
Author

well. clicked on my empty page to hit that play/pause button. it seems that it is working now like it should. will come back again when i redid the css stuff.

@rafa8626
Copy link
Contributor

rafa8626 commented Nov 7, 2016

perfect. Keep me posted. Thanks

@NitroxydeX
Copy link
Author

NitroxydeX commented Nov 7, 2016

Well yeah, it's working now.

Updated your code. Was the wrong order of removing and adding classes and you were missing closing });

$(window).load(function() {

        var
                target = $('#audio-player').closest('.mejs__container'),
                playerId = target.attr('id'),
                player = mejs.players[playerId],
                button = target.find('.mejs__playpause-button'),
                i = Math.floor( ( Math.random() * 1000000 ) + 1),
                source = 'http://thgfm.de:8000/dsl;',
            ;

        button.on('click', function(e) {

            if (player.media.readyState > 0) {
                player.pause();
                player.setSrc('');
                player.load();
                button.removeClass('mejs__pause').addClass('mejs__play');
            } else {
                i++;
                player.setSrc(source + '?nocache=' + i);
                player.load();
                player.play();
                button.removeClass('mejs__play').addClass('mejs__pause');
            }

            return false;
        });
 });

@NitroxydeX
Copy link
Author

but what is this "first" thing for?

@rafa8626
Copy link
Contributor

rafa8626 commented Nov 7, 2016

What first thing?

@rafa8626
Copy link
Contributor

rafa8626 commented Nov 7, 2016

I'm glad is working for you. Can this ticket be closed?

@NitroxydeX
Copy link
Author

this: 'first = true;' as a variable. you added it but what it does?

@rafa8626
Copy link
Contributor

rafa8626 commented Nov 7, 2016

Oh, that's a typo from old code I was testing. You can safely ignore that and remove it. My apologies

@NitroxydeX
Copy link
Author

Ah well okay ^^
yeah I think it can be closed now because we fixed the problem.

@rafa8626
Copy link
Contributor

rafa8626 commented Nov 7, 2016

@NitroxydeX Thanks for your help on this one. I'll go and close #613 as well

@rafa8626
Copy link
Contributor

rafa8626 commented Nov 7, 2016

@NitroxydeX Just FYI we reverted the change with the BEM naming convention so keep that in mind when you download the latest version of the player. Use the class names you used before. Sorry for the inconveniences

@NitroxydeX
Copy link
Author

.... well okay :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants