2012-07-09 283 views

回答

1

在YouTube pluginApi的setSrc方法不存在或不正確:https://github.com/johndyer/mediaelement/issues/381

你要麼必須直接調用loadVideoByUrl()或loadVideoById()從pluginApi內:

var myVideoPlayer = new MediaElementPlayer('#elementId'); 
myVideoPlayer.media.pluginApi.loadVideoById('myYoutubeId'); 

或者創建一個插件,將延長「媒體」指標,並添加setSrc()來調用這些方法之一:

/* global mejs */ 

(function($) { 
    $.extend(MediaElementPlayer.prototype, { 
     buildmyyoutube: function(player, controls, layers, media) { 
      if (!player.isVideo || media.pluginType !== 'youtube') { 
       return; 
      } 

      $.extend(media, { 
       /** 
       * mejs' youtube plugin's setSrc doesn't support youtube urls (the flash one does) 
       * 
       * @param string urlOrId Can either be a youtube url http://www.youtube.com/watch?v=<YoutubeId> 
       *      or directly a youtube id 
       */ 
       setSrc: function (urlOrId) { 
        var queryStr = urlOrId.match(/v=([^&]*)/); 
        var id = urlOrId; 

        if(queryStr !== null) { 
         id = queryStr[1]; 
        } 
        this.pluginApi.loadVideoById(id); 
       } 
      }); 
     } 
    }); 
})(mejs.$); 

var myVideoPlayer = new MediaElementPlayer('#elementId', {features: ['myyoutube']}); 
myVideoPlayer.setSrc('myYoutubeId'); 
myVideoPlayer.setSrc('http://www.youtube.com/watch?v=myYoutubeId'); 

我沒有測試它原樣,我不得不從我使用的一些代碼編輯它,但它應該工作。