View source
<?php
function html5_media_theme() {
$themes = array();
$themes['html5_player'] = array(
'render element' => 'element',
);
$info = html5_media_get_player_info();
foreach ($info['templates'] as $name => $info) {
$themes['html5_player_' . $name] = array(
'template' => 'minplayer_' . $name,
'variables' => array(
'params' => NULL,
),
'path' => $info['path'],
);
}
return $themes;
}
function html5_media_library() {
$path = drupal_get_path('module', 'html5_media') . '/player';
return array(
'html5_player' => array(
'title' => 'Media Player',
'version' => '0.1',
'js' => array(
$path . '/bin/minplayer.compressed.js' => array(
'group' => JS_LIBRARY,
),
),
'dependencies' => array(
array(
'system',
'ui.slider',
),
),
),
'html5_player_debug' => array(
'title' => 'Media Player (Debug Mode)',
'version' => '0.1',
'js' => array(
$path . '/src/minplayer.compatibility.js' => array(
'group' => JS_LIBRARY,
),
$path . '/src/minplayer.flags.js' => array(
'group' => JS_LIBRARY,
),
$path . '/src/minplayer.async.js' => array(
'group' => JS_LIBRARY,
),
$path . '/src/minplayer.plugin.js' => array(
'group' => JS_LIBRARY,
),
$path . '/src/minplayer.display.js' => array(
'group' => JS_LIBRARY,
),
$path . '/src/minplayer.js' => array(
'group' => JS_LIBRARY,
),
$path . '/src/minplayer.image.js' => array(
'group' => JS_LIBRARY,
),
$path . '/src/minplayer.file.js' => array(
'group' => JS_LIBRARY,
),
$path . '/src/minplayer.playLoader.js' => array(
'group' => JS_LIBRARY,
),
$path . '/src/minplayer.players.base.js' => array(
'group' => JS_LIBRARY,
),
$path . '/src/minplayer.players.html5.js' => array(
'group' => JS_LIBRARY,
),
$path . '/src/minplayer.players.flash.js' => array(
'group' => JS_LIBRARY,
),
$path . '/src/minplayer.players.minplayer.js' => array(
'group' => JS_LIBRARY,
),
$path . '/src/minplayer.players.youtube.js' => array(
'group' => JS_LIBRARY,
),
$path . '/src/minplayer.players.vimeo.js' => array(
'group' => JS_LIBRARY,
),
$path . '/src/minplayer.controller.js' => array(
'group' => JS_LIBRARY,
),
),
'dependencies' => array(
array(
'system',
'ui.slider',
),
),
),
);
}
function html5_media_get_player_info() {
$cache = cache_get('html5_player_info');
if ($cache) {
return $cache->data;
}
else {
$player_info = module_invoke_all('html5_player_info');
cache_set('html5_player_info', $player_info);
return $player_info;
}
}
function html5_media_html5_player_info() {
$path = drupal_get_path('module', 'html5_media') . '/player/templates';
return array(
'plugins' => array(),
'templates' => array(
'default' => array(
'path' => $path . '/default',
'js' => array(
$path . '/default/js/minplayer.playLoader.default.js' => array(
'group' => JS_DEFAULT,
),
$path . '/default/js/minplayer.controller.default.js' => array(
'group' => JS_DEFAULT,
),
$path . '/default/js/minplayer.default.js' => array(
'group' => JS_DEFAULT,
),
),
'css' => array(
$path . '/default/css/minplayer_default.css' => array(
'group' => CSS_DEFAULT,
),
),
),
'jqueryui' => array(
'path' => $path . '/jqueryui',
'js' => array(
$path . '/jqueryui/js/minplayer.playLoader.jqueryui.js' => array(
'group' => JS_DEFAULT,
),
$path . '/jqueryui/js/minplayer.controller.jqueryui.js' => array(
'group' => JS_DEFAULT,
),
$path . '/jqueryui/js/minplayer.jqueryui.js' => array(
'group' => JS_DEFAULT,
),
),
'css' => array(
$path . '/jqueryui/css/minplayer_jqueryui.css' => array(
'group' => CSS_DEFAULT,
),
),
),
),
);
}
function html5_media_player_settings() {
return array(
"id" => 'player',
"controller" => 'default',
"template" => 'default',
"swfplayer" => '',
"wmode" => 'transparent',
"preload" => true,
"autoplay" => false,
"loop" => false,
"width" => '100%',
"height" => '400px',
"debug" => false,
"volume" => 80,
"files" => array(),
"file" => '',
"preview" => '',
"attributes" => array(),
);
}
function html5_media_register_player($settings, $attributes) {
$playerId = $settings['id'];
html5_media_add_resources($settings['template'], $settings['debug']);
$attributes = drupal_json_encode($attributes);
$settings = array_intersect_key($settings, html5_media_player_settings());
$settings = trim(drupal_json_encode($settings), '{}');
$swfplayer = url(drupal_get_path('module', 'html5_media') . '/player/flash/minplayer.swf');
drupal_add_js("\n jQuery(function() {\n jQuery('#{$playerId}').minplayer({\n id:'#{$playerId}',\n attributes:{$attributes},\n {$settings},\n swfplayer:'{$swfplayer}'\n });\n });\n ", 'inline');
}
function theme_html5_player($variables) {
if (isset($variables['element'])) {
$element =& $variables['element'];
}
else {
$element & $variables;
}
$settings = $element['#settings'];
$attributes = $element['#attributes'];
if (empty($element['#sources'])) {
return 'No media sources provided';
}
$element['#value'] = '';
foreach ($element['#sources'] as $delta => $file) {
$file = (object) $file;
if ($source = html5_media_get_source($file)) {
$element['#value'] .= theme('html_tag', array(
'element' => array(
'#tag' => 'source',
'#attributes' => array(
'src' => $source,
),
),
));
}
}
$variables['player'] = theme('html_tag', $variables);
$variables['settings'] = $settings;
$variables['params'] = $settings;
html5_media_register_player($settings, $attributes);
return theme('html5_player_' . $settings['template'], $variables);
}
function html5_media_get_source($file) {
if ($file) {
if (isset($file->uri)) {
return file_create_url($file->uri);
}
else {
if (!empty($file->value)) {
return $file->value;
}
}
}
return '';
}
function html5_media_get_extension($file) {
if ($source = html5_media_get_source($file)) {
return drupal_strtolower(drupal_substr($source, strrpos($source, '.') + 1));
}
return '';
}
function html5_media_get_media_type($file) {
if (isset($file->filemime)) {
if (strpos($file->filemime, 'video/') === 0) {
return 'video';
}
if (strpos($file->filemime, 'audio/') === 0) {
return 'audio';
}
}
if ($ext = html5_media_get_extension($file)) {
if (in_array($ext, array(
'swf',
'mov',
'mp4',
'm4v',
'flv',
'f4v',
'ogg',
'ogv',
'3g2',
'webm',
))) {
return 'video';
}
if (in_array($ext, array(
'mp3',
'oga',
'wav',
'aif',
'm4a',
'aac',
))) {
return 'audio';
}
}
return !empty($file->value) ? 'video' : '';
}
function html5_media_field_formatter_info() {
return array(
'html5_player' => array(
'label' => t('Media Player'),
'description' => t('Play this file within a Media Player.'),
'field types' => array(
'file',
'text',
),
'settings' => array(
'template' => 'default',
'preload' => TRUE,
'autoplay' => FALSE,
'loop' => FALSE,
'width' => '100%',
'height' => '400px',
'volume' => 80,
'sources' => FALSE,
'debug' => FALSE,
),
),
);
}
function html5_media_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$element = array();
if ($display['type'] == 'html5_player') {
$info = html5_media_get_player_info();
$templates = array_keys($info['templates']);
$templates = array_combine($templates, $templates);
$element['template'] = array(
'#title' => t('Template'),
'#type' => 'select',
'#options' => $templates,
'#default_value' => $settings['template'],
);
$element['preload'] = array(
'#title' => t('Preload'),
'#type' => 'checkbox',
'#default_value' => $settings['preload'],
);
$element['autoplay'] = array(
'#title' => t('Autoplay'),
'#type' => 'checkbox',
'#default_value' => $settings['autoplay'],
);
$element['loop'] = array(
'#title' => t('Loop'),
'#type' => 'checkbox',
'#default_value' => $settings['loop'],
);
$element['width'] = array(
'#title' => t('Width'),
'#type' => 'textfield',
'#default_value' => $settings['width'],
);
$element['height'] = array(
'#title' => t('Height'),
'#type' => 'textfield',
'#default_value' => $settings['height'],
);
$element['volume'] = array(
'#title' => t('Initial Volume (0 - 100)'),
'#type' => 'textfield',
'#default_value' => $settings['volume'],
);
$element['sources'] = array(
'#title' => t('Allow multiple sources'),
'#description' => t('Checking this will turn multiple instances of files into multiple sources within the media element.'),
'#type' => 'checkbox',
'#default_value' => $settings['sources'],
);
$element['debug'] = array(
'#title' => t('Debug Mode'),
'#type' => 'checkbox',
'#default_value' => $settings['debug'],
);
}
return $element;
}
function html5_media_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = '';
if ($display['type'] == 'html5_player') {
$header = array(
'Setting',
'Value',
);
$rows = array();
foreach ($settings as $name => $value) {
$rows[] = array(
$name,
$value,
);
}
$summary = theme('table', array(
'header' => $header,
'rows' => $rows,
));
}
return $summary;
}
function html5_media_get_attributes($settings) {
$attributes = array();
$element_settings = array(
'preload',
'autoplay',
'loop',
);
foreach ($settings as $name => $value) {
if ($value && in_array($name, $element_settings)) {
$attributes[$name] = NULL;
}
}
$attributes['id'] = $settings['id'] . '-player';
$attributes['class'] = 'minplayer-' . $settings['template'] . '-media';
$attributes['width'] = '100%';
$attributes['height'] = '100%';
return $attributes;
}
function html5_media_add_resources($template, $debug) {
static $resources_added = FALSE, $template_added = array();
$info = html5_media_get_player_info();
if (!$resources_added) {
drupal_add_library('html5_media', $debug ? 'html5_player_debug' : 'html5_player');
foreach ($info['plugins'] as $plugin) {
if ($plugin['js']) {
foreach ($plugin['js'] as $file => $options) {
drupal_add_js($file, $options);
}
}
if ($plugin['css']) {
foreach ($plugin['css'] as $file => $options) {
drupal_add_css($file, $options);
}
}
}
}
$templates = $info['templates'];
if (!isset($template_added[$template]) && isset($templates[$template])) {
$template_added[$template] = TRUE;
$template = $templates[$template];
if ($template['js']) {
foreach ($template['js'] as $file => $options) {
drupal_add_js($file, $options);
}
}
if ($template['css']) {
foreach ($template['css'] as $file => $options) {
drupal_add_css($file, $options);
}
}
}
}
function html5_media_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
switch ($display['type']) {
case 'html5_player':
$settings = $display['settings'];
$id = 'player-' . drupal_clean_css_identifier($field['field_name']);
if ($settings['sources']) {
$mediatag = '';
foreach ($items as $delta => $item) {
if ($mediatag = html5_media_get_media_type((object) $item)) {
break;
}
}
if ($mediatag) {
$settings['id'] = $id;
$element[$delta] = array(
'#theme' => 'html5_player',
'#tag' => $mediatag,
'#attributes' => html5_media_get_attributes($settings),
'#settings' => $settings,
'#sources' => $items,
);
}
}
else {
foreach ($items as $delta => $item) {
if ($mediatag = html5_media_get_media_type((object) $item)) {
$settings['id'] = $id . '-' . $delta;
$element[$delta] = array(
'#theme' => 'html5_player',
'#tag' => $mediatag,
'#attributes' => html5_media_get_attributes($settings),
'#settings' => $settings,
'#sources' => array(
$item,
),
);
}
}
}
break;
}
return $element;
}