MediaElement.js Change source of video onclick

柔情痞子 提交于 2019-12-03 02:11:37
Matthew Bergsma

If you are using the player with the API, you can use the "setSrc" method.

Example:

<script>
var player = new MediaElementPlayer('video', {
    defaultVideoWidth: 960,
    defaultVideoHeight: 410,
    features: ['playpause', 'progress', 'current', 'duration', 'volume', 'fullscreen'], 
    success: function (mediaElement, domObject) {}
});

// ... sometime later

player.setSrc('urlToVid.mov');
player.play();
</script>

You can't just change the source of the video file. You have to remove the mejs div, create a new html5 video, and then call mediaelement on it again. By repeating this process, you're having it generate a new flash fallback with each video, as necessary.

it worked for me to load the media object after the src had been changed .. like so:

var player = new MediaElementPlayer('#vid'/*, Options */);        

$('#the_url').on('keypress',function(e){    
    if (e.which == 13) {
        player.pause();
        player.setSrc($('#the_url').val());        
        player.media.load();
    }
});

Old post - figured I'd add my resolution

Note: calling load() will also auto-play the video. This code also assumes that your input has a value already in there, and it is a child of the anchor tag that was clicked.

$('#idThatSwitches').click(function (e) {
    // get youTubeId
    var youTubeId = $(this).find('input[type="hidden"]').val();

    // switch out player's video source and reload
    var player = $('#idOfYourPlayer')[0].player.media;
    player.setSrc('http://youtube.com/watch?v=' + youTubeId);
    player.load();
});

Since this has taken a lot of time for me to figure out, I'll post what I've found here. I used the MediaElementJS Wordpress plug-in. To change the source, you can get a reference to the media element player like this:

$('#wp_mep_1')[0].player.media

The reason for doing this is because in WordPress the plug-in doesn't give you a variable to call functions like .load() or .play() on. So, I finally figured out that was how you can get a reference to the player inserted by the plug-in.

So to change the src:

var player = $('#wp_mep_1')[0].player.media;
if(player.setSrc)
    player.setSrc('xyz.mp4');

I'll take my turn at resurrecting an old question as well.

I'm not sure if something has changed in the MediaElement plugin, but none of the above solution worked for me 100%, some worked partially and some didn't work at all. A couple of the issues I faced were I could only load video into an element if there was not already a previous video loaded and playing, this was no good for me as it was meant to be used as a playlist selector. The other issue was for whatever reason the video would not load all the way and just stop a few seconds in.

The below however is working for me as of Oct '15.

<a href="http://www.youtube.com/watch?v=VIDEO-ID" onclick="
  playerObject.pause();
  playerObject.setSrc(this.href);
  playerObject.media.load();
  playerObject.load();
  return false
">
<img src="yourimg">
</a>

I like to use A tage to help me with formatting clickable objects like this, also preserves open in new window/tab functions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!