Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping instead of rewinding at the end of a video in MediaElement.js

I'm wondering how to stop the MediaElement.js player at the end of the video. I wondered how to stop the mediaelement.js player at the end of a video. I was hoping to hold on the last frame and not rewind to show the first frame as it does now.

Is it possible to change this behaviour?

like image 770
Don McMillan Avatar asked Feb 10 '11 21:02

Don McMillan


4 Answers

I wrote a fix for this problem and John merged in version 2.10.2. There is now an option "autoRewind" that you can set to false to prevent the player from going back to the beginning. The eventlistener is not added and there is no more need to remove it.

$('video').mediaelementplayer({
    autoRewind: false
});
like image 95
Simon Schuh Avatar answered Nov 10 '22 03:11

Simon Schuh


I believe that the default behavior of the <video> element is to go back to the beginning so you'd just need to override this by listening for the ended event.

var player = $('#myvideo').mediaelementplayer();

player.media.addEventListener('ended', function(e) {
    player.media.setCurrentTime(player.media.duration);
}, false);

Hope that helps!

like image 34
John Dyer Avatar answered Nov 10 '22 01:11

John Dyer


Probably the best solution is not to be afraid and remove the "rewind-to-start-on-video-end" handler from mediaelement source.

If you go into the source code for mediaelement and search for "ended", you'll eventually see, that rewinding after reaching end of the video is actually done deliberately by mediaelement.

If you want to remove that functionality feel free to just remove that handler for "ended" event from mediaelement source. That solves all the problems, including flickering between last and first frame, mentioned in some other answers to this question.

like image 3
WTK Avatar answered Nov 10 '22 02:11

WTK


The code in John Dyer's answer didn't really work for me either for some reason. I was however able to get this version working...

var videoPlayer = new MediaElementPlayer('#homepage-player', {
    loop: false,
    features:[],
    enablePluginDebug: false,
    plugins: ['flash','silverlight'],
    pluginPath: '/js/mediaelement/',
    flashName: 'flashmediaelement.swf',
    silverlightName: 'silverlightmediaelement.xap',

    success: function (mediaElement, domObject) { 
        // add event listener
        mediaElement.addEventListener('ended', function(e) {
            mediaElement.pause();
            mediaElement.setCurrentTime(mediaElement.duration);
        }, false);
    },
    error: function () { 
    }

});

videoPlayer.play();

The only problem I'm having - which is very frustrating, is it is flickering between the LAST and FIRST frames in Chrome. Otherwise, it works as expected in Firefox and IE...

like image 1
TwstdElf Avatar answered Nov 10 '22 02:11

TwstdElf