Files
王阳 ce4b31470c 初始提交:MoePlayer Moodle media 插件
- media_moeplayer_plugin 接管 mp4/m4v/mov/webm/ogv/m3u8/flv/ts,rank=100
- 播放器资源内打包(moeplayer/ 目录),每页一次加载,多视频多实例
- 管理设置:皮肤(默认/红色/西瓜)、默认宽高
- 中英语言包,兼容 Moodle 3.9+(3.x/4.x)
2026-07-21 16:51:21 +08:00

115 lines
3.9 KiB
PHP

<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* MoePlayer 播放器 media 插件
*
* 把页面中的视频链接/嵌入媒体交给 MoePlayer 渲染。
* 播放器资源(js/css/语言包/解码库)随插件打包,无需额外部署。
*
* @package media_moeplayer
* @copyright MoePlayer 项目(fork 自 ckplayer, www.ckplayer.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* MoePlayer media 插件类
*/
class media_moeplayer_plugin extends core_media_player {
/** @var array 支持的扩展名 */
private static $extensions = ['mp4', 'm4v', 'mov', 'webm', 'ogv', 'm3u8', 'flv', 'ts'];
/**
* 支持的扩展名列表(core_media_manager 据此匹配视频链接)
*
* @return array
*/
public function get_supported_extensions() {
return self::$extensions;
}
/**
* 优先级,高于内置 html5video/videojs,使 MoePlayer 优先接管
*
* @return int
*/
public function get_rank() {
return 100;
}
/**
* 生成嵌入代码
*
* @param array $urls 媒体 URL 列表(moodle_url)
* @param string $name 名称
* @param int $width 宽度
* @param int $height 高度
* @param array $options 选项
* @return string HTML
*/
public function embed($urls, $name, $width, $height, $options) {
global $PAGE;
// 页面只需加载一次播放器资源(多视频共存时去重)
static $loaded = false;
if (!$loaded) {
$base = new moodle_url('/media/player/moeplayer/moeplayer');
$skin = get_config('media_moeplayer', 'skin');
if (!in_array($skin, ['moeplayer.css', 'moeplayer.red.css', 'moeplayer.ixigua.css'])) {
$skin = 'moeplayer.css';
}
$PAGE->requires->css($base . '/css/' . $skin);
$PAGE->requires->js($base . '/js/moeplayer.min.js', true);
$loaded = true;
}
$videourl = addslashes($urls[0]->out(false));
$id = html_writer::random_id('moeplayer_');
// 尺寸:优先使用管理器传入值,其次插件默认设置
$width = $width ?: get_config('media_moeplayer', 'width') ?: '100%';
$height = $height ?: get_config('media_moeplayer', 'height') ?: '480';
$container = html_writer::tag('div', '', [
'id' => $id,
'class' => 'media-moeplayer',
'style' => 'width:' . (is_numeric($width) ? $width . 'px' : $width) . ';'
. 'height:' . (is_numeric($height) ? $height . 'px' : $height) . ';max-width:100%;',
]);
// 等播放器 js 就绪后初始化(handlebars/JS 过滤等场景下加载顺序不确定)
$script = html_writer::script("
(function() {
var init = function() {
if (typeof MoePlayer === 'undefined') {
return setTimeout(init, 50);
}
new MoePlayer({
container: '#{$id}',
video: '{$videourl}'
});
};
init();
})();
");
return $container . $script;
}
}