Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

media filter


Go to End
Reply


1025 Views

Avatar
Anatol

126 Posts

11 August 2011 at 2:43pm

Edited: 11/08/2011 2:47pm

Hi,

I just thought this might be worth sharing. I created a media filter that parses the page content for any links to media files (such as .mp4, .m4v, .flv, etc.) and then automatically embeds a player.

This is just some very quick code that can be improved a lot and I might write a proper module/extension for this if I find the time, but so far it this seems to work well.

Put this into your Page class in /mysite/code/Page.php

function Content() {
	return $this->MediaFilter($this->Content);
}
	
function MediaFilterCallback($matches) {
	$mediaplayer_id = 'mediaplayer-'.md5($matches[2].$matches[3]);

	$output = $matches[0].'<br />';
	$output .= '<div id="'.$mediaplayer_id.'"></div>';
	$output .= '<script type="text/javascript">';
	$output .= "jwplayer('".$mediaplayer_id."').setup({";
	$output .= "'flashplayer': 'mediaplayer/player.swf',";
	$output .= "'image': 'mediaplayer/preview.jpg',";
	$output .= "'file': '".$matches[2].".".$matches[3]."',";
	$output .= "'controlbar': 'over',";
	$output .= "'duration': '34',";
	$output .= "'fullscreen': 'true',";
	$output .= "'screencolor': 'eeeeee',";
	$output .= "'stretching': 'uniform',";
	$output .= "'width': '540',";
	$output .= "'height': '405'";
	$output .= "});";
	$output .= "</script>";
	
	return $output;
}

function MediaFilter($subject) {
	// links with the following file extensions will be replaced by an embedded player
	$search = '/<a(.*?)href=\"([^<]+)\.(m4v|mp4|mov|flv|3gp|3g2|mp3|aac|m4a)\"([^>]*)>(.*?)<\/a>/is';
	return preg_replace_callback($search,array('Page','MediaFilterCallback'),$subject);
}

Add this line into the <head> of your template file /themes/mytheme/templates/Page.ss :

<script type="text/javascript" src="mediaplayer/jwplayer.js"></script>

Then download the JWPlayer into your silverstripe directory, unzip and name the downloaded folder "mediaplayer".

Done. All code should now get parsed for media links and be replaced by an embedded media player.

As I said this could still be improved a lot, i.e. create a proper module with a config file for file types and player configuration, Requirements::javascript, etc. I'll post this if I get around to do that.

Cheers!
Anatol