Don McMillan
Don McMillan

Reputation: 67

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?

Upvotes: 5

Views: 7227

Answers (5)

Amr Alaa
Amr Alaa

Reputation: 553

This problem i faced when playing audio files

The problem is in the play, when you pause your player the file will stop but before resuming you have to decrease the current time of the player by any value in your case you may decrease it by a frame maybe

after setting your source ,loading your file and pausing, then

    myplayer.player.play();
    var currentTime = myplayer.player.getCurrentTime();
    myplayer.player.setCurrentTime(currentTime-0.1);
    myplayer.player.setCurrentRail();

Upvotes: 0

Simon Schuh
Simon Schuh

Reputation: 171

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
});

Upvotes: 9

TwstdElf
TwstdElf

Reputation: 171

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...

Upvotes: 1

WTK
WTK

Reputation: 16971

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.

Upvotes: 3

John Dyer
John Dyer

Reputation: 1005

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!

Upvotes: 3

Related Questions