Extension:FlashMP3

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual
Crystal Clear action run.png
FlashMP3

Release status: beta

Implementation Tag
Description Allows to play mp3-files in an embedded Flash-player
Author(s) Matthias Korn
Latest version v0.92 (2012-01-20)
Database changes No
License GNU General Public License
Download See the code section
Example RiC Wiki
Tags
<flashmp3>

Translate the FlashMP3 extension if it is available at translatewiki.net

Check usage and version matrix; code metrics

The FlashMP3 extension embeds a simple Flash-player for playback of mp3-files. It is based on the great Audio Player Wordpress plugin by 1pixelout. It can handle multiple local and/or remote files in one player and multiple players on each site. The appearance of the player is highly customizable.

It is also possible to use the nice last.fm player with the files hosted there. It works on a label, artist, album or song-level.

For problems and suggestions please use the discussion page of this article.

Installation[edit | edit source]

  1. Open a new text (notepad) file.
  2. Copy & paste the code below in the newly created text (notepad) file.
  3. Save the file as flashmp3.php
  4. Place it in a new folder called FlashMP3 in your extensions directory.
  5. Download the standalone Audio Player plugin from 1pixelout.
  6. Open the downloaded plugin.
  7. From the zip file copy the files audio-player.js and player.swf.
  8. Place the audio-player.js and player.swf in the folder FlashMP3 you just created.
  9. Near the end of your LocalSettings.php file in the MediaWiki root folder add this line:
    require_once('extensions/FlashMP3/flashmp3.php');
    

Uploading mp3-files
To be able to upload mp3-files to your MediaWiki also do the following steps.

  1. Allow uploading of mp3-files by adding this line near the end of your LocalSettings.php.
    $wgFileExtensions[] = 'mp3';
    
  2. Set the maximum size for file uploads in your php.ini.
  3. Set the maximum size for file uploads by adding this line to your LocalSettings.php. The value is the maximum size of the file in bytes.
    $wgMaxUploadSize = '52428800';
    

Code[edit | edit source]

You can change the Default Title ($restTitle) and the Default Album Cover ($albumArt) in line 87 and line 91 respectively to your needs (for last.fm player only).

<?php
 
if ( ! defined( 'MEDIAWIKI' ) )
	die();
 
/*******************************************************************************
*                                                                              *
* FlashMP3 Extension by Matthias Korn to embed a flash player with mp3-files   *
* https://www.mediawiki.org/wiki/Extension:FlashMP3                             *
*                                                                              *
* This extension uses the Audio Player Wordpress plugin from 1pixelout         *
* Download and unpack files audio-player.js and player.swf to your             *
* extensions/FlashMP3 folder                                                   *
*                                                                              *
*   http://www.1pixelout.net/code/audio-player-wordpress-plugin/               *
*                                                                              *
* Tag :                                                                        *
*   <flashmp3>mp3</flashmp3>                                                   *
*                                                                              *
* Example :                                                                    *
*   with a local file                                                          *
*   <flashmp3>music.mp3</flashmp3>                                             *
*                                                                              *
* Example :                                                                    *
*   with a remote file                                                         *
*   <flashmp3>http://www.somedomain.com/mp3/music.mp3</flashmp3>               *
*                                                                              *
********************************************************************************/
 
$wgExtensionFunctions[] = 'wfFlashMP3';
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'FlashMP3',
        'description' => 'Plays mp3-files in an embedded Flash-player',
        'author' => 'Matthias Korn',
        'url' => 'https://www.mediawiki.org/wiki/Extension:FlashMP3',
        'version' => 'v0.92'
);
 
function wfFlashMP3() {
        global $wgParser;
        $wgParser->setHook('flashmp3', 'renderFlashMP3');
}
 
// The callback function for converting the input text to HTML output
function renderFlashMP3($input, $args) {
	global $wgScriptPath;
	$type = '1pixelout';
	$id = 1;
        $params = explode("|", htmlspecialchars($input));
        $files = explode(",", array_shift($params));
        $player_path = $wgScriptPath.'/extensions/FlashMP3/';
 
        if (isset($args['type'])) {
           $type = $args['type'];
        }
 
        switch ($type) {
 
			case 'lastfm':
 
				// concat the parameter string
				$typeParameter = array_shift($params);	// this is the resourceType
				$param_string = implode('&amp;', $params);
				if (!empty($param_string)) {
					$param_string = '&amp;'.$param_string;
				}
 
				// try to figure out the resourceType
				$resourceTypes['artist']   =  6;
				$resourceTypes['album']    =  8;
				$resourceTypes['song']	   =  9;
				$resourceTypes['playlist'] = 10;
				$resourceTypes['label']	   = 10;
 
				if (is_numeric($typeParameter)) {
					$resourceType = $typeParameter;
				} else {
					$resourceType = $resourceTypes[$typeParameter];
				}
 
				// the resourceID to play (either from an artist, song, playlist, ...)
				$resourceID = $files[0];
 
				// setting default title and cover
				if (strpos($param_string, 'restTitle=') === false) {
					$restTitle = '&amp;restTitle=Default Title';	// TODO: you can set a default title here
				}
				if (strpos($param_string, 'albumArt=') === false) {
					$albumArt = '&amp;albumArt=http://static.last.fm/depth/catalogue/noimage/cover_med.gif';	// TODO: you can set a default cover here
				}
 
				$output .= '<object width="340" height="123">'
					. '<param name="movie" value="http://static.last.fm/webclient/41/defaultEmbedPlayer.swf" />'
					. '<param name="FlashVars" value="viral=true&amp;lfmMode=playlist&amp;resourceID='.$resourceID.'&amp;resourceType='.$resourceType.$albumArt.$restTitle.$param_string.'" />'
					. '<param name="wmode" value="transparent" />'
					. '<embed src="http://static.last.fm/webclient/41/defaultEmbedPlayer.swf" width="340" '
					. '  FlashVars="viral=true&amp;lfmMode=playlist&amp;resourceID='.$resourceID.'&amp;resourceType='.$resourceType.$albumArt.$restTitle.$param_string.'"'
					. '  height="123" wmode="transparent" type="application/x-shockwave-flash" />'
					. '</object>';
 
				break;
 
			case '1pixelout':
 
			default:
 
        		if (isset($args['id']) && preg_match('/^[a-z0-9]+$/iD', $args['id'])) {
	           		$id = $args['id'];
        		}
 
				// concat the file string for all files
				$file_string = '';
				$j = 0;
				foreach ($files as $file) {
					// get the intenral path if file is uploaded
				        if (strpos($file, "http://") !== 0 && strpos($file, "https://") !== 0) {
					    $title = Title::newFromText($file,NS_IMAGE);
					    if($title == NULL){
						return FlashMP3NotFoundError($file);
					    }   	
					    $img = function_exists( 'wfFindFile' ) ? wfFindFile( $title ) : new Image( $title );
 
					    if ( $img ) {
						$file = $img->getURL(); 
					    } else {
						return flashMP3NotFoundError($file);
					    }
					}
					$file_string .= $file;
					if ($j < count($files)-1) $file_string .= ',';
					$j++;
				}
 
				// concat the parameter string
				$param_string = implode('&amp;', $params);
				if (!empty($param_string)) $param_string .= '&amp;';
 
				$output = '<script language="JavaScript" src="'.$player_path.'audio-player.js"></script>'
					. '<object type="application/x-shockwave-flash" data="'.$player_path.'player.swf" id="audioplayer'.$id.'" height="24" width="290">'
					. '<param name="movie" value="'.$player_path.'player.swf">'
					. '<param name="FlashVars" value="playerID='.$id.'&amp;'.$param_string.'soundFile='.$file_string.'">'
					. '<param name="quality" value="high">'
					. '<param name="menu" value="false">'
					. '<param name="wmode" value="transparent">'
					. '</object>';
 
				break;
        }
 
        return $output;
}
 
function flashMP3NotFoundError($input) {
    return '<div style=\'border: solid red 1px\'>
<p align=center><b>Error: file <code>'.$input.'</code> not found </b> </p><br>
</div>';
}

Usage[edit | edit source]

<flashmp3>filename.mp3|option=value|...</flashmp3>

filename.mp3 can be the name of a file uploaded to the Wiki or an external URL. Additional parameters are optional.

Options[edit | edit source]

Option Effect
autostart=yes The player will automatically open and start to play the track (default value is no)
loop=yes The track will be looped indefinitely (default value is no)
bg=0xHHHHHH Background colour option (where HHHHHH is a valid hexadecimal colour value such as FFFFFF or 009933)
leftbg=0xHHHHHH Left background colour
rightbg=0xHHHHHH Right background colour
rightbghover=0xHHHHHH Right background colour (hover)
lefticon=0xHHHHHH Left icon colour
righticon=0xHHHHHH Right icon colour
righticonhover=0xHHHHHH Right icon colour (hover)
text=0xHHHHHH Text colour
slider=0xHHHHHH Slider colour
loader=0xHHHHHH Loader bar colour
track=0xHHHHHH Progress track colour
border=0xHHHHHH Progress track border colour

Multiple files[edit | edit source]

Multiple files can be played in one FlashMP3 player one after the other by separating them with comma.

Multiple players[edit | edit source]

Multiple FlashMP3 players on one site can be achieved by giving them an id.

<flashmp3 id="2">secondmp3.mp3</flashmp3>

Examples[edit | edit source]

<flashmp3>mymp3.mp3</flashmp3>

Plays an uploaded mp3-file called mymp3.mp3.

<flashmp3>http://www.somedomain.com/mp3/mymp3.mp3</flashmp3>

Plays the remote mp3-file from the URL http://www.somedomain.com/mp3/mymp3.mp3.

<flashmp3>mymp3.mp3,yourmp3.mp3|autostart=yes|loop=yes</flashmp3>

Plays the two uploaded files mymp3.mp3 and yourmp3.mp3 one after the other, autostarts playing when the site is loaded and loops the files.

Usage for last.fm player[edit | edit source]

<flashmp3 type="lastfm">resourceID|resourceType|parameter=value|...</flashmp3>

resourceID and resourceType have to be retrieved from the source of the last.fm page of which you want to embed the player (Right Click -> View Page Source and search for resourceID and resourceType). Both resourceID and resourceType are compulsory. Additional parameters are optional.

resourceType is either artist, album, song, label, playlist or the ID-number directly (if you know it).

Options[edit | edit source]

Option Effect resourceType
restTitle= The title shown after the player loaded (default value can be set in the PHP code below) all
albumArt= URL of the cover shown after the player loaded (default value can be set in the PHP code below) all
labelName= Name of the label label, playlist
firstTrackName= Name of the first track played (will be shown even before the play button is pressed) artist, album, song
firstArtistName= Name of the first artist played (will be shown even before the play button is pressed) artist, album, song
restDuration= Duration of the track (will be shown even before the play button is pressed) artist, album, song

Additional parameter are possible as long as the last.fm player can interpret them in the given situation (i.e. resourceType).

Examples[edit | edit source]

<flashmp3 type="lastfm">11572702|artist</flashmp3>

Plays songs from the given artist (Tookoo).

<flashmp3 type="lastfm">51022335|song|firstTrackName=Dance+For+Yourself|firstArtistName=TOOKOO|albumArt=http://static.last.fm/coverart/130x130/3245162.jpg|restDuration=227</flashmp3>

Plays the given song and displays the track name, artist name and duration as well as the cover right after loading.

Limitations[edit | edit source]

  • The default player has a white background at the beginning when still folded. This is a limitation of the 1pixelout player and can not be changed. (Firefox on Linux only!?)
  • For the last.fm player you have to search the source code to find the resourceID. To my knowledge there is no better way to do this at the moment.
  • More than 15 players embedded on a page will not work under Firefox
  • Can not recognize Unicode file names
  • Files used by FlashMP3 will show up in the Special:Unused Files
  • Resource-hostile. Can lag the reader's computer easily if too many MP3 files embedded in the same page.
    • When this becomes a severe issue of your wiki, think of converting your MP3 files to Vorbis OGG audio format and use the more resource-friendly OGG player.

Changelog[edit | edit source]

  • v0.92 (2012-01-20)
    • Combatible with MW v1.18.1
  • v0.91 (2007-04-03)
    • fixed some possible Cross-site scripting vulnerabilities
  • v0.8 (2007-02-25)
    • initial release