Autoplay MediaElementPlayer

守給你的承諾、 提交于 2019-11-30 06:57:49

This is a bug in MediaElementJS, autoplay works for native and silverlight, but needs a little help with Flash.

You can listen for canPlay event and start playing as soon as the flash player is ready.

setTimeout tricks may fail in race conditions.

$('#playerid').mediaelementplayer({
    plugins: ['flash', 'silverlight'],
    success: function(mediaElement, domObject) {
        if (mediaElement.pluginType == 'flash') {
            mediaElement.addEventListener('canplay', function() {
                // Player is ready
                mediaElement.play();
            }, false);
        }
    },
    error: function() {
        alert('Error setting media!');
    }
});
Nagkumar Arkalgud

You can add the autoplay="true" attribute to the <video> tag as shown below:

<video width="100%" height="auto" poster="some_image.jpg" controls="controls" 
    autoplay="true">

  <source type="video/mp4" src="myvideo.mp4" />
  <source type="video/webm" src="myvideo.webm" />
  <source type="video/ogg" src="myvideo.ogv" />
  <object width="320" height="240" type="application/x-shockwave-flash" data="flashmediaelement.swf">
    <param name="movie" value="flashmediaelement.swf" />
    <param name="flashvars" value="controls=true&file=myvideo.mp4" />
    <img src="myvideo.jpg" width="320" height="240" title="No video playback capabilities" />
  </object>
</video>

To play it on all the browsers/devices, you can use jQuery to reference the object and call its play() method.

<script>  
     var player = new MediaElementPlayer('#id-of-player',/* Options */);
     player.play(); 
</script>

For further information, you can check here.

This can be helpfull.

$('audio,video').mediaelementplayer({
        success: function(player, node) {
        $('.mejs-overlay-button').trigger('click');
        }
});
// works here (tested with MediaElements.js 2.1.9)
$(document).ready(function () {
  $('video,audio').mediaelementplayer().load();
});

Unless i miss the point just add 'autoplay' to HTML5 video tag

var player = new MediaElementPlayer('video', {
    startVolume: 0.8,
    features: ['playpause','progress'],
    autoplay: true,
    audioWidth: 250,
    audioHeight: 40
    }
);


player.play();

just create your own object and start it via play();

Although user932056's suggestion didn't exactly work for me, I used the general idea at the top of my page which finally worked:

<script type="text/javascript">
$(document).ready(function() {
   $("div.mejs-container .mejs-button").trigger('click');
});
</script>

Here is code which I wrote and works for me in HTML5 video cases and for flash player too;

$('video,audio').mediaelementplayer({
   mode: 'auto_plugin',
   defaultVideoWidth: 480, 
   defaultVideoHeight: 360,
   success: function (me) {
      whenPlayerReadyToPlay(me, function () {
         me.play();
      })
   }
});

function whenVideoReadyToPlay(me, callback) {
   if (me.pluginType !== 'flash') {
      me.addEventListener('loadstart', function (e) {
         callback();
      }, false);

      return;
   }

   if (me.attributes.preload === 'none') {
      $(me.pluginElement).ready(function (e) {
         callback();
      });
   }
   else {
      me.addEventListener('canplay', function (e) {
         callback();
      }, false);
   }
}

here is the code:

$(document).ready(function () {

        $('video,audio').mediaelementplayer({ defaultVideoWidth: 480, defaultVideoHeight: 360 });

        setTimeout('eventClickTrigger()', 1000);
    });

    function eventClickTrigger() {

        $('.mep-overlay').trigger('click');
    }

simple, but took me a week

Here is the simplest way I know. I'm not sure if this is what you're looking for but if my understanding of your question is correct, this worked for me. I am using MediaElement 2.9.4.

<script>
    MediaElement('player1', {success: function(me) {    
    me.play();
    }});
</script>
Mevo

Or you could have put autoplay and/or preload="auto" to the <video> tag that the jQuery above is referencing.

For those who have an issue with autoplay on safari iOS (or any other browser that rejecting autoplay ) there is a new policy that require a user action to play media(audio, video), any of the solutions mentioned above will not work. more details here https://webkit.org/blog/6784/new-video-policies-for-ios/.

The best workaround is to detect if the video is not playing using the success callback and then append a button with an action to play the video

var video = document.querySelector('video');
var promise = $j('.video-wrap .video').mediaelementplayer({
        autoplay: true,
        playsinline: 1,
        success: function(media, node, player) {
          if(media.paused){
                $( "body" ).append( "<button type="button" id='playvideo'>Play</button>" );
          }
        },
        error: function (e) {
        }
});
//add a click action to play video  
$( "#playvideo" ).click(function() {
        video.play()
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!