如何在fancybox中安装MediaElement播放器?

时间:2012-05-21 06:09:52

标签: jquery fancybox fancybox-2 mediaelement.js

HTML:

{% for movie in videos %}
    <div class="list_item">            
        <a class="open-fancybox" href="#fancybox">Play</a>
        <div id="fancybox">
          <video id="#mediaelement" src="{{ movie.file }}" preload=none></video>
        </div>      
    </div>   
{% endfor %}

JS:

$(document).ready(function() {           

        $('.open-fancybox').fancybox({
            'afterShow': function() {
                $('#mediaelement').mediaelementplayer();
            }
        });

});

萤火虫没有错误。问题是fancybox将整个页面作为内容而不是视频。

知道出了什么问题吗?

3 个答案:

答案 0 :(得分:3)

最好动态加载视频,而不是嵌入内联,所以我会这样做:

<a title="video title" data-poster="images/thumb.jpg" href="videos/video.mp4" class="fancy_video"><img alt="" src="images/thumb.jpg" /></a>

注意我们直接在href属性中链接到(mp4)视频。另请注意(HTML5)data-poster属性,该属性指示将在视频容器空闲时显示的缩略图图像。然后是基本的jQuery代码:

// set some general variables
var $video_player, _player, _isPlaying = false;
jQuery(document).ready(function ($) {
    jQuery(".fancy_video").fancybox({
        // set type of content (we are building the HTML5 <video> tag as content)
        type: "html",
        // other API options
        scrolling: "no",
        fitToView: false,
        autoSize: false,
        beforeLoad: function () {
            // build the HTML5 video structure for fancyBox content with specific parameters
            this.content = "<video id='video_player' src='" + this.href + "' poster='" + $(this.element).data("poster") + "' width='360' height='360' controls='controls' preload='none' ></video>";
            // set fancyBox dimensions
            this.width = 360; // same as video width attribute
            this.height = 360; // same as video height attribute
        },
        afterShow: function () {
            // initialize MEJS player
            var $video_player = new MediaElementPlayer('#video_player', {
                defaultVideoWidth: this.width,
                defaultVideoHeight: this.height,
                success: function (mediaElement, domObject) {
                    _player = mediaElement; // override the "mediaElement" instance to be used outside the success setting
                    _player.load(); // fixes webkit firing any method before player is ready
                    _player.play(); // autoplay video (optional)
                    _player.addEventListener('playing', function () {
                        _isPlaying = true;
                    }, false);
                } // success
            });
        },
        beforeClose: function () {
            // if video is playing and we close fancyBox
            // safely remove Flash objects in IE
            if (_isPlaying && navigator.userAgent.match(/msie [6-8]/i)) {
                // video is playing AND we are using IE
                _player.remove(); // remove player instance for IE
                _isPlaying = false; // reinitialize flag
            };
        }
    });
}); // ready

参见 JSFIDDLE

注意

  • 虽然jsfiddle使用mediaelement.js v2.13.2(最新的cdnjs),但我总是建议更新到包含错误修复的最新版本(v2.14.2)。
  • 该演示还使用fancyBox v2.1.15并仅涵盖mp4视频文件。

有关详细的完整代码说明,包括已知问题和解决方法,请参阅本教程http://www.picssel.com/play-mp4-videos-with-mediaelement-js-in-fancybox/

答案 1 :(得分:2)

嗨,我认为你应该删除id值的哈希值:

<div id="fancybox">代替<div id="#fancybox">

答案 2 :(得分:1)

我试图使用mediaelement wordpress插件做这样的事情。我最终得到了类似的东西:

$('a.fancybox').fancybox({
    'afterShow': function() {
        mejs.$('video').mediaelementplayer();
    }
});