Magento & jPlayer: Play Nice Together!

Email to a friend Email to a friend
 
  Recommend Recommend

...Rockin’ to the Music with HTML 5 and Flash

By Paul Briscoe


Have you ever wanted to play media samples on Magento product pages that are manageable directly through the product admin? Well, by default, Magento will use the built-in browser functionality to play media samples that open in a separate window. But this is not always an elegant solution, and now, with new versions of FireFox not supporting MP3 in the HTML5 audio tag, we need a better solution.

Read more on this issue:
Mozilla defends Firefox's HTML5 support for only Ogg Theora video

Enter jPlayer, a “completely free and open source (GPL/MIT) media library written in JavaScript.” The jPlayer jQuery plugin “allows you to rapidly weave cross platform audio and video into your web pages.” (Chek out jPlayer.org) And not only is the support forum maintained by an excellent staff, but this baby is easy to install and get running quickly on your site.

Can you say awesome? 

What I would like to discuss in this article is one implementation of jPlayer with Magento. The goal of the implementation was to be able to play samples of downloadable MP3s within a player on the product page. I did some research on Magento extensions to do this before I thought about implementing jPlayer, but what I found was that the extensions weren’t necessarily going to do what I wanted them to, or they were expensive and this was intended to be a low-budget task.

In the end I decided on jPlayer because I wanted it to have the support for multiple filetypes and media formats. That is, if we decided to use some video later on it would be supported with the same plugin. (Plus, it was free and only required a bit of development to get going.)

Here are a couple extensions I looked at before I decided on using jPlayer.
MageWorld MP3 Player
Desv.us MP3 Player

Now let’s take a look at the implementation.
First, I had to make sure that my site is using jQuery and I downloaded, installed, and included the jPlayer correctly. Here is the installation location of jQuery/jPlayer and the entries in my page.xml




skin_jsjs/jquery.1.6.2.min.js
skin_jsjs/jQuery.jPlayer.2.1.0/jquery.jplayer.js
skin_jsjs/jQuery.jPlayer.2.1.0/add-on/jplayer.playlist.js
skin_jsjs/jQuery.jPlayer.2.1.0/add-on/jquery.jplayer.inspector.js

The next step was looking at how the samples are rendered on the product page. One of the goals was to use jQuery on the page without any sort of admin areas. So if there are MP3 samples loaded into the product, jQuery will sniff them out and manipulate the DOM to create a JSON playlist from the samples, generate the player HTML, and finally load the playlist into the player and display it on the page.

Breaking it apart into steps looks like this:

Step 1: Use a jQuery selector to detect if there are samples in the product
This is the first part of the script that finds the MP3 and creates the JSON we will use for the playlist.


Step 2: Create a variable to hold the MP3 player html
If you go to jPlayer.org you can view the source on the page to grab the HTML, basically the concept is to set up the HTML in the javascript so that we do not have to include the MP3 player in the template.

We can choose to add it in only if samples are available.
Along these lines:


Step 3: Initiate the jPlayer, Create a Playlist Object, Evaluate our JSON, and finally load it up.
This is the final step and really is pretty standard operating procedure for the playlist.


Now if we take a look at a product page that has sample MP3s you should see the jPlayer load up the tracks. That’s it! We successfully implemented a version of jPlayer on the Magento product page for local Ann Arbor record label Oddfellow Music, which loads a sample MP3 into the player. Awesome!






As I mentioned before, there are many ways to implement the jPlayer and this is just one version. I hope this helps anyone looking to use jPlayer as a method to play Magento MP3 Samples! As always, if you have any questions or issues, post a comment and I’ll try to help you out!


Happy Coding,

The Office Minstrel

Comments (7)

Maybe try a custom option
Hi Matt, If you have a simple product you might be able to add a Product Attribute that store the URL linking to your sample. To give it a shot you... Read more
4/4/2012 1:02 PM
pbriscoe
help please
How do I make the player show up where it needs to go. I mean I have done everything in this tutorial... including making the sample.js file which to... Read more
4/4/2012 1:42 AM
jdf
Adding Audio Samples to non-downloadable products
Awesome post! This seems to be ALMOST exactly what I'm looking for... We sell MP3s and CDs on our site, and I am looking for a method to add audio s... Read more
3/22/2012 2:38 PM
Matt
Regarding mp3 player adding
Hi Paul, Its very nice tutorial, Thanks a lot for sharing. Please alsso add the following lines to this. <code> <action method="addIte... Read more
3/3/2012 8:19 AM
Ravi Choudhary
Great tutorial
Can this code be downloaded? as I need this feature on my Magento site? What version of Magento will this work on?
3/1/2012 8:41 PM
Leon
Web site creation
So as a novice programmer I can duplicate this code and insert my own MP3 play web site?
2/7/2012 12:48 PM
Mike Lorenz