A while back I was impressed to see the Yahoo Media Player, which can be used to stream mp3s on a website. It appeared on the face of it to be a JavaScript only browser media player, which seemed not only impressive, but impossible. As it turned out, the media player actually used flash underneath the hood by making a series of API calls without the need for the typical flash UI.
Being suitably impressed I decided to write my own – I wanted several distinct features like a playlist and a custom look. I then found an astonishing good JavaScript API that interfaced to the audio features in Flash. This library was called SoundManager2, check it out, it’s great. SoundManager does provide some neat demo applications, but nothing that matched my needs – incidentally the demos may be far better now as I wrote this code almost a year ago.
So what was I looking to achieve with this:
- I wanted to queue tracks in a playlist;
- The order of the playlist must be dynamic, i.e. you can drag and drop the tracks to change their order;
- Adding the media player to a website is as easy as adding a div with a specific id attribute;
- Adding items to a playlist must be as simple as including an anchor tag with a specific CSS class;
- The colours, dimensions, and images should be easily overridable but also have sensible defaults;
- The path to the library should be configurable in some manner.
So what did this turn out like? Well you can see it in action on www.feellikefalling.com. I encourage you to check it out to see things like the play list drag and drop etc in action. Update: I have embedded the player here so that you don’t need to navigate away if you don’t want to.
Your browser does not support iframes.
So if you want to include this on your website how to you do it? It’s pretty easy – you don’t need to be a web developer! First you need to download all the required files by clicking the big button above or here, extract the files, and then upload the these files to your web server. The HTML required to embed the player is minimal and is show below:
1 2 3 4 5 6 7 8 9 10 11 12 | <div id="myplayer"> <a class="myplayer_mp3" href="song1.mp3" title = "Feel Like Falling - Can't Rock With The Kids"> <span class="myplayer_artist">Feel Like Falling</span> - <span class="myplayer_song">Can't Rock With The Kids</span> </a> <a class="myplayer_mp3" href="song2.mp3" title = "Feel Like Falling - Purple T"> <span class="myplayer_artist">Feel Like Falling</span> - <span class="myplayer_song">Purple T</span> </a> </div> |
To include the website media player in the page you simply add a div
element with id myplayer
. Then to add an mp3 to the playlist you create an anchor (a
) tag with class myplayer_mp3
. To include a tooltip for the song you simply add a title
attribute to the a
element. So what we are doing is creating a playlist from a series of urls specified in the a
tags, it’s as simple as that.
In order to get all this to work you need to include the following in your document header:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <link rel="stylesheet" type="text/css" href="js/myplayer/player.css"> <!--You must specify the location, relative to this file, where the myplayer source can be found. This allows it to set up some things automatically for you --> <script type="text/javascript" charset="utf-8"> var _MyPlayerLocation_ = "js/myplayer"; </script> <!-- It uses jQuery so load the main jQuery library however you wish in this case I'm using Google to host it --> <script type="text/javascript" src="http://www.google.com/jsapi"></script> <script type="text/javascript"> google.load("jquery", "1.3.2"); </script> <!--The rest are JavaScript file that are required by the media player and should be included in the order specified - they are all contained in the downloadable zip file. All you have to do is make sure you have specified the correct path relative to this file --> <script type="text/javascript" src="js/myplayer/jquery/jquery-ui-custom.min.js"></script> <script type="text/javascript" src="js/myplayer/jquery/jquery.tooltip.js"></script> <script type="text/javascript" src="js/myplayer/soundmanager/soundmanager2-min.js"></script> <script type="text/javascript" src="js/myplayer/utils.js"></script> <script type="text/javascript" src="js/myplayer/myplayer.min.js"></script> |
Instead of explaining all of the above I suggest you just take a look at the inline comments. There is also a sample HTML file in the downloadable zip containing full working example – apart from the actual mp3 sound files.
You can also customise the player visually with relative ease. The colour scheme can be changed by simply overriding the default CSS styles for the player. For example, consider the following screenshot:
Here the volume bars, the play timer, the currently playing song background, and main background colour have all been changed. The code below shows the CSS elements that had to be changed/created to achieve this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | div#myplayer { background-color: #696969; height: 25em; } #myplayer_playlist_container { height: 20em; } div#myplayer_playing_timer { background-color: #6D0D33; } div.myplayer_strip_skin_on { background-color: #6D0D33; } #myplayer_playlist li { background-color: #929292; color: #FFFFFF; } a.myplayer_mp3 { color: #000000; } #myplayer_playlist li.ui-selected { background-color: #6D0D33; } #myplayer_playlist li.ui-selected a { color: #FFFFFF; } |
Hopefully the method of skinning the player, as shown above, is straightforward to understand, it’s just basic CSS. Obviously just about anything can be customised through the use of CSS, and I suggest taking a look at the generated HTML to identify the appropriate ids and classes – you can use firebug HTML explorer with Firefox (or the equivalent in IE, Chrome, Safari, etc). In fact, the HTML generated can also be modified easily – take a look at the myplayer_snippets.js
to do this. Be aware though that some of the elements generated are essential to the working of the application though.
For those more artistically inclined than me feel free to give it a sweeping new skin – in the colours of the site I linked to so that I can get the benefits 😉
It goes without saying there will be bugs! If you find any, either leave a comment or better still email me – my address can be found in the about section of this site.
So feel free to add the webpage media player to your site! Enjoy.