调整动态加载的 MediaElementjs 视频的大小

Moi*_*tah 4 html javascript video resize mediaelement

所以我有一个视频,我用 jQuery 的 .load() 加载到页面中。这是您可以在“视频”部分查看问题的网站:http : //guillaumep.com/

这是我必须使用的视频标签:

<video><source src="URLTOVIDEO.mp4" type="video/mp4" />
Run Code Online (Sandbox Code Playgroud)

这是在加载到我的视频后立即生成视频 HTML 的代码#contentFrame

$('video').wrap('<div class="videowrapper">')

.attr({ 'style': 'width: 100%; height: 100%;' })
.mediaelementplayer({ success: function (player) { 
    correctVideoSize($(player).parent(4).eq(0)); }
});
Run Code Online (Sandbox Code Playgroud)

然后我有代码根据窗口的形状实际调整视频播放器的大小,以便视频始终以最大尺寸显示,而不会溢出窗口:

function correctVideoSize(videoContainer) {
    var videoContainerHeight = $(window).height() - 100;
    var videoContainerWidth = $(window).width() * 0.92;
    var videoContainerRatio = videoContainerWidth / videoContainerHeight;
    var videoRatio = videoContainer.width() / videoContainer.height();
    console.log('videoRatio : ' + videoRatio);
    console.log('videoContainerRatio : ' + videoContainerRatio);

    if (!isNaN(videoRatio) && videoContainerRatio < videoRatio)
    {
        videoContainer.height(videoContainerWidth / videoRatio).width(videoContainerWidth);
    }
    else if (!isNaN(videoRatio))
    {
        videoContainer.height(videoContainerHeight).width(videoContainerHeight * videoRatio);
    }

    return videoContainer;
}
Run Code Online (Sandbox Code Playgroud)

以这种方式调整图像大小绝对有效,正如您在那里看到的:http : //guillaumep.com,但对于视频,我不明白我应该如何、何时、何地以及在何处调整大小。您可以在此处查看问题:http : //guillaumep.com/2012/10/22/test-video/

我尝试了很多不同的事情,并让视频在 $(window).resize() 事件上正确调整大小,但我想这只是因为 MediaElementJs 已经挂接到这个事件并做了一些魔术。

我真的迷路了,谢谢你的帮助!

Nic*_*ico 5

我这里有点晚了。在我的谷歌搜索结果中发现了这个,因为我遇到了和你一样的问题。实际上,窗口大小调整确实触发了 de mediaplayer 调整大小的正确事件。如果你真的想要一个简单的解决方案,你可以像这样手动触发该事件:(jquery)

$(window).trigger('resize'); 
Run Code Online (Sandbox Code Playgroud)

我将查看源代码,看看是否可以为我们找到更好的解决方案。现在,手动触发它可以解决您的问题。

编辑

显然,所有调整大小绑定的作用是:

mediaelement.setPlayerSize();
mediaelement.setControlsSize();
Run Code Online (Sandbox Code Playgroud)

那应该更直接地解决您的问题。祝你好运!

  • 这个答案应该得到1000个赞。 (2认同)