You are here

jplayer.module in jPlayer 6

Same filename and directory in other branches
  1. 8.2 jplayer.module
  2. 7.2 jplayer.module

Provides an HTML5-compatible with Flash-fallback audio player.

This module provides functionality for loading the jPlayer library and formatters for CCK FileFields.

File

jplayer.module
View source
<?php

/**
 * @file
 * Provides an HTML5-compatible with Flash-fallback audio player.
 *
 * This module provides functionality for loading the jPlayer library and
 * formatters for CCK FileFields.
 */

/**
 * Implementation of hook_menu().
 */
function jplayer_menu() {
  $items = array();
  $items['admin/settings/jplayer'] = array(
    'title' => 'jPlayer settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'jplayer_settings_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'description' => 'Configure the settings for the jPlayer module.',
    'file' => 'includes/jplayer.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_theme().
 */
function jplayer_theme() {
  return array(
    'jplayer_formatter_single' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'includes/jplayer.theme.inc',
    ),
    'jplayer_single' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'template' => 'theme/jplayer',
      'file' => 'includes/jplayer.theme.inc',
    ),
    'jplayer_formatter_playlist' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'file' => 'includes/jplayer.theme.inc',
    ),
    'jplayer_playlist' => array(
      'arguments' => array(
        'element' => NULL,
      ),
      'template' => 'theme/jplayer',
      'file' => 'includes/jplayer.theme.inc',
    ),
    'jplayer_view_playlist' => array(
      'arguments' => array(
        'view' => NULL,
        'items' => NULL,
      ),
      'template' => 'theme/jplayer',
      'file' => 'includes/jplayer.theme.inc',
    ),
  );
}

/**
 * Implementation of Views' hook_views_api().
 */
function jplayer_views_api() {
  return array(
    'api' => 2,
    'path' => drupal_get_path('module', 'jplayer') . '/includes',
  );
}

/**
 * Implementation of CCK's hook_field_formatter_info().
 */
function jplayer_field_formatter_info() {
  return array(
    'single' => array(
      'label' => t('jPlayer player'),
      'field types' => array(
        'filefield',
        'audiofield',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
      'description' => t('Display an audio file as an HTML5-compatible with Flash-fallback audio player.'),
    ),
    'playlist' => array(
      'label' => t('jPlayer multifile playlist'),
      'field types' => array(
        'filefield',
        'audiofield',
      ),
      'multiple values' => CONTENT_HANDLE_MULTIPLE,
      'description' => t('Display multi-value fields as an HTML5-compatible with Flash-fallback audio player.'),
    ),
  );
}

/**
 * Add the jPlayer library to the page.
 *
 * @param $add
 *   By default this function will add jPlayer to the page JavaScript array
 *   directly. If wanting to store the jPlayer file as an #attached property,
 *   set this to FALSE and jplayer_add() will only return the needed array
 *   suitable for use as an #attached property.
 */
function jplayer_add($add = TRUE) {
  static $added = FALSE;
  $directory = variable_get('jplayer_directory', 'sites/all/libraries/jplayer');
  $return = FALSE;
  if (file_exists($directory . '/jquery.jplayer.min.js')) {
    $filepath = $directory . '/jquery.jplayer.min.js';
  }
  elseif (file_exists($directory . '/jquery.jplayer.js')) {
    $filepath = $directory . '/jquery.jplayer.js';
  }
  if (isset($filepath)) {
    $jplayer_js = jplayer_get_file_path('jplayer.js');
    $jplayer_css = jplayer_get_file_path('jplayer.css');
    $settings = array(
      'jPlayer' => array(
        'swfPath' => base_path() . variable_get('jplayer_directory', 'sites/all/libraries/jplayer'),
        'autoPlay' => (int) variable_get('jplayer_autoplay', ''),
        'pauseOthers' => variable_get('jplayer_pause_others', FALSE),
        'protected' => variable_get('jplayer_protected', ''),
      ),
    );
    if ($add) {
      drupal_add_js($filepath);
      drupal_add_js($jplayer_js);
      drupal_add_css($jplayer_css);
      if (!$added) {
        drupal_add_js($settings, 'setting');
        $added = TRUE;
      }
    }
    $return = array(
      'js' => array(
        array(
          'data' => $filepath,
        ),
        array(
          'data' => $jplayer_js,
        ),
        array(
          'data' => $settings,
          'type' => 'setting',
        ),
      ),
      'css' => array(
        array(
          'data' => $jplayer_css,
        ),
      ),
    );
  }

  // Allow other modules to add resources to the page when a jPlayer is
  // embedded.
  if ($additional_resources = module_invoke_all('jplayer_add_js')) {
    $return['additional_resources'] = $additional_resources;
    if ($add) {
      foreach ($additional_resources as $resource) {
        $js_info = $resource['#attached']['js'];
        foreach ($js_info as $js_path => $js_parameters) {
          drupal_add_js($js_path, $js_parameters['type'], $js_parameters['scope']);
        }
      }
    }
  }
  return $return;
}

/**
 * Returns path to the most appropriate file 
 *
 * @param $filename
 *   Name of the file to find
 * @param $required_files
 *   Other files required in target folder
 */
function jplayer_get_file_path($filename, $required_files = array()) {
  global $theme;
  $theme_dir = drupal_get_path('theme', $theme) . '/jplayer/';
  $found = false;
  if (file_exists($path = $theme_dir . $filename)) {
    $found = true;
    foreach ($required_files as $key => $value) {
      if (!file_exists($theme_dir . $value)) {
        $found = false;
        break;
      }
    }
  }
  return $found ? $path : drupal_get_path('module', 'jplayer') . '/theme/' . $filename;
}

/**
 * Return the version of jQuery UI installed.
 *
 * @param $directory
 *   The directory to check for a jPlayer installation.
 */
function jplayer_get_version($directory = NULL) {
  $version = 0;
  if (!isset($directory)) {
    $directory = variable_get('jplayer_directory', 'sites/all/libraries/jplayer');
  }
  if (file_exists($directory . '/jquery.jplayer.min.js')) {
    $contents = file_get_contents($directory . '/jquery.jplayer.min.js');
  }
  elseif (file_exists($directory . '/jquery.jplayer.js')) {
    $contents = file_get_contents($directory . '/jquery.jplayer.js');
  }
  $matches = array();
  preg_match('/Version:?[ ]*([\\d.]+)/i', $contents, $matches);
  if (isset($matches[1])) {
    $version = $matches[1];
  }
  return $version;
}

Functions

Namesort descending Description
jplayer_add Add the jPlayer library to the page.
jplayer_field_formatter_info Implementation of CCK's hook_field_formatter_info().
jplayer_get_file_path Returns path to the most appropriate file
jplayer_get_version Return the version of jQuery UI installed.
jplayer_menu Implementation of hook_menu().
jplayer_theme Implementation of hook_theme().
jplayer_views_api Implementation of Views' hook_views_api().