Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MP3 media handler for MediaWiki
<?php
# Stream MP3 using HTML5 <audio> tag
$wgMediaHandlers['audio/mp3'] = 'MP3MediaHandler';
$wgFileExtensions[] = 'mp3';
$wgExtensionCredits['parserhook'][] = array(
'name' => 'MP3MediaHandler',
'description' => 'Provides an in-page mp3 player.',
'author' => "Mark Clements (HappyDog)",
'version' => '1.0',
'url' => 'https://www.kennel7.co.uk/testwiki'
);
class MP3MediaHandler extends MediaHandler {
function validateParam( $name, $value ) { return true; }
function makeParamString( $params ) { return ''; }
function parseParamString( $string ) { return array(); }
function normaliseParams( $file, &$params ) { return true; }
function getImageSize( $file, $path ) { return false; }
# Prevent "no higher resolution" message.
function mustRender( $file ) { return true; }
function getParamMap() { return array(); }
function doTransform ($file, $dstPath, $dstUrl, $params, $flags = 0) {
return new MP3OutputRenderer($file->getFullUrl(), $file->getTitle());
}
}
class MP3OutputRenderer extends MediaTransformOutput {
var $pSourceFileURL;
var $pFileName;
function __construct($SourceFileURL, $FileName){
$this->pSourceFileURL = $SourceFileURL;
$this->pFileName = $FileName;
}
function toHtml($options=array ()) {
$Output = '<audio controls="controls">'
. '<source src="$1" type="audio/mp3" />'
. $this->getFlashPlayerHTMLTemplate('<p><a href="$1">$2</a></p>')
. '</audio>';
$Args = array(
'$1' => $this->pSourceFileURL,
'$2' => $this->pFileName,
);
return $this->expandHtml($Output, $Args);
}
function getFlashPlayerHTMLTemplate($NonFlashFallback) {
global $wgFlashPlayerPath, $wgFlashPlayerURLParam, $wgFlashPlayerParams;
if (isset($wgFlashPlayerPath)) {
// A common default parameter name for the audio file to be loaded is 'url',
// so we default to this. Individual implementations can over-ride via
// LocalSettings.php, if necessary.
if (!isset($wgFlashPlayerURLParam)) {
$wgFlashPlayerURLParam = "url";
}
$ExtraParams = "";
if (is_array($wgFlashPlayerParams)) {
foreach ($wgFlashPlayerParams as $Param => $Value) {
$ExtraParams .= '<param name="' . htmlspecialchars($Param)
. '" value="' . htmlspecialchars($Value) . '">';
}
}
$HTML = '<object data="$10" type="application/x-shockwave-flash">'
. '<param name="movie" value="$10" />'
. '<param name="$11" value="$1" />'
. $ExtraParams
. $NonFlashFallback
. '</object>';
$Args = array(
'$10' => $wgFlashPlayerPath,
'$11' => $wgFlashPlayerURLParam,
);
return $this->expandHtml($HTML, $Args);
}
else {
return $NonFlashFallback;
}
}
function expandHtml($HTML, $Args) {
foreach ($Args as $Key => $Value) {
$Args[$Key] = htmlspecialchars($Value);
}
return str_replace(array_keys($Args), array_values($Args), $HTML);
}
}
?>
@MarkMaldaba

This comment has been minimized.

Copy link
Owner Author

MarkMaldaba commented Oct 16, 2015

Initial draft: Copy of code at https://www.mediawiki.org/wiki/Extension:MiniMp3
Subsequent drafts: Modifying to use HTML5 audio tag, instead.
Ideal: audio tag, with Flash fallback if unsupported, with simple anchor tag to file download if neither supported.

@MarkMaldaba

This comment has been minimized.

Copy link
Owner Author

MarkMaldaba commented Oct 16, 2015

Updated to provide a fallback for non-HTML5 browsers (link to file).

@MarkMaldaba

This comment has been minimized.

Copy link
Owner Author

MarkMaldaba commented Oct 17, 2015

Updated to provide basic (untested) Flash fallback. Requires SWF file to be added and configured as part of deployment.

@MarkMaldaba

This comment has been minimized.

Copy link
Owner Author

MarkMaldaba commented Feb 13, 2016

Now made into a proper extension, and housed in the Wikimedia git repo.

See extension page at mediawiki.org for full details: https://www.mediawiki.org/wiki/Extension:MP3MediaHandler

@zoglun

This comment has been minimized.

Copy link

zoglun commented Nov 7, 2016

Hi MarkMaldaba, I am running a npo wiki farm and find out that your extension solve the mp3 play problem under ios system (flash not available) the best. However, would you mind to improve your extension with the flash fall back from https://www.mediawiki.org/wiki/Extension:FlashMP3 ? This extension using the best flash player for mp3 I have ever used so far.
Would be really appreciated if you could combine this flash fallback with options.

In any case I am sincerely thank you for wrote this extension and publish it.

@maxlefou

This comment has been minimized.

Copy link

maxlefou commented Nov 30, 2016

For some odd reason, it doesn't work for me. It still displays the link for the file's page instead of a player. But the extension is active anyway. Though, my wiki isn't in english. Has this script been tested on non-english wikis?

@MarkMaldaba

This comment has been minimized.

Copy link
Owner Author

MarkMaldaba commented Jul 3, 2017

Apologies that I've only just seen these comments. Github didn't alert me to your posts.

@zoglun - You can configure which Flash player to use with this extension. It should work with the one you name, but let me know if it doesn't.

@maxlefou - I am not aware of any reason this should not work on non-English wikis. Please try the latest version and if you still have issues let me know.

In all cases, please do not respond here, as I may not see it. The latest version of the code and the best places to report any issues can both be found at the extension's project page on MediaWiki.org: https://www.mediawiki.org/wiki/Extension:MP3MediaHandler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.