jPlayer

HTML5 Audio & Video for jQuery

a project by happyworm

jPlayer Fundamentals

Flash Security Rules

JPlayer plugin's SWF file must be on your domain.

You cannot link to the jPlayer's SWF file on happyworm.com as this violates the same domain policy.

You need to upload the files to a directory called "js" on your domain. If required, use the constructor option swfPath to change the path.

Technically, the JS file of the plugin may be linked to remotely at happyworm.com, but you will need to have a local copy of the Jplayer.swf file on your domain.

jPlayer Constructor

$(id).jPlayer( Object: options )

Description
The jPlayer constructor is applied to the given id and uses the options, if provided.
Parameters
options
Object defining any changes to the default configuration of jPlayer.
ready :
Function : (Default: undefined) : Defines a function to call when the jPlayer plugin is ready for use.
Generally, it is recommended to use a function here to at least .change(f) the jPlayer to a valid mp3 file ready for use. This is defined at creation to eliminate a possible race condition. Ensuring that the function definition exists when Flash triggers it's ready event.
cssPrefix
String : (Default: "jqjp") : Defines the prefix for jPlayer's CSS class manipulation.
See jPlayer CSS Classes for more information.
swfPath
String : (Default: "js") : Defines the relative path of the jPlayer plugin's SWF file.
This allows the developer to place the jPlayer's SWF file in an alternative relative path.
position
String : (Default: "absolute") : Defines the CSS position style of the Flash.
left
String : (Default: "0") : Defines the CSS left style of the Flash.
top
String : (Default: "0") : Defines the CSS top style of the Flash.
width
String : (Default: "0") : Defines the width of the Flash.
height
String : (Default: "0") : Defines the height of the Flash.
quality
String : (Default: "high") : Defines the quality of the Flash.
bgcolor
String : (Default: "#ffffff") : Defines the background color of the Flash.

HTML entry, with an example id for jPlayer:

<head>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js">
  </script>
  <script type="text/javascript" src="js/jquery.jplayer.js>
  </script>
  <script> $(document).ready(function() { /* Your Code */ });
  </script>
</head>
<body>
  <div id="jpId"></div>
</body>

Code Example #1:

$(document).ready(function() {
  $("#jpId").jPlayer();
});

Code Example #2:

$(document).ready(function() {
  $("#jpId").jPlayer( {
    ready: function () {
	  $("#jpId").changeAndPlay("../mp3/elvis.mp3");
	},
	cssPrefix: "my_jp_class"} );
  });
});

jPlayer Methods

jPlayer.jPlayerId( String: methodName, String: methodCssId )

jPlayer.jPlayerId( String: methodName ) : String

Description
This jPlayerId method is used to create or read associations between jPlayer methods and CSS entities on the webpage.
Creation associates a handler method inside the plugin with a CSS Id on the webpage. This enables an element on the webpage to be associated with the method that, for example, executes the play command.
Reading returns the CSS Id associated with the method.
Parameters
methodName
String containing the name of the method to associate with the methodCssId.
"play"
the method that plays the mp3
"pause"
the method that pauses the mp3
"stop"
the method that stops the mp3
"loadBar"
the method that listens to and manipulates the load bar
"playBar"
the method that manipulates the play bar
"volumeMin"
the method that mutes the volume
"volumeMax"
the method that maximises the volume
"volumeBar"
the method that listens to the volume bar
"volumeBarValue"
the method that manipulates the volume's value
"bufferMsg"
the method that outputs a buffer message
methodCssId
(Optional) String containing the name of the CSS Id to associate with the methodName
This parameter is optional. If ommitted, the CSS Id of the associated method is returned.
Requesting an unknown or unset method CSS Id generates a warning alert popup and returns a false.

Code Examples:

$("#jpId").jPlayerId( "play", "thePlayButton" );
$("#jpId").jPlayerId( "loadBar", "theLoadBar" );
myPauseButtonId = $("#jpId").jPlayerId( "pause" );

jPlayer.change( String: filename )

Description
This method is used to define the MP3 file to play.
The .change() method must be used to define an MP3 file before the jPlayer will be able perform other methods like .play().
The plugin does not begin downloading the new file defined by the .change() method.
Any song being played when the command is issued will be stopped.
Parameters
filename
String defining the path to the MP3 file
The URL given must conform to standard URL Encoding Rules.
Flash Security Rules apply here for loading the given URL.

Code Examples:

$("#jpId").change( "mp3/elvis.mp3" );
$("#jpId").change( "../mp3/elvis.mp3" );

jPlayer.changeAndPlay( String: filename )

Description
This method is used to define an MP3 file and to begin playing the new file.
The new file will begin downloading immediately.
Any song being played when the command is issued will be stopped.
Parameters
filename
String defining the path to the MP3 file
See jPlayer.change( String: filename ) for more information

Code Examples:

$("#jpId").changeAndPlay( "mp3/elvis.mp3" );
$("#jpId").changeAndPlay( "../mp3/elvis.mp3" );

jPlayer.play()

Description
This method is used to play the MP3 file.
If necessary, the file will begin downloading.
Play will either begin from the start of the MP3 or from where the play-head was when previously paused.
Parameters
none
This method has no parameters.

Code Examples:

$("#jpId").play();

jPlayer.pause()

Description
This method is used to pause the MP3 file.
When paused, the play-head time is stored in the plugin.
Parameters
none
This method has no parameters.

Code Examples:

$("#jpId").pause();

jPlayer.stop()

Description
This method is used to stop the MP3 file.
The play-head will be reset to the start of the song.
Parameters
none
This method has no parameters.

Code Examples:

$("#jpId").stop();

jPlayer.playHead( Number: percentOfLoaded )

Description
This method moves the play head to a new position
Parameters
percentOfLoaded
Number (0 to 100) defining the percentage played when compared to the current percentage downloaded.
Only when completely downloaded does the percentage relate to the total length of the mp3 file.
Eg. A 4 minute song, that is 50% downloaded and given the percentOfLoaded of 50% would move to 25% into the total song, which is at 1 minute.
See jPlayer.onProgressChange( Function: handleChanges ) for information on gaining access to loadPercent and playPercentRelative values.

Code Examples:

$("#jpId").playHead(0); // Begins playing at the start.
$("#jpId").playHead(10); // Begins playing at 10% of the downloaded length.
$("#jpId").playHead(100); // Begins playing at the downloaded length, causing either buffering if downloading or ending if loaded.

jPlayer.volume( Number: percent )

Description
This method is used to control the volume of the MP3 file being played.
Parameters
percent
Number defining the percentage of maximum volume.
To mute the volume use: 0
For half volume use: 50
For maximum volume use: 100

Code Examples:

$("#jpId").volume( 25 );
$("#jpId").volume( volumeValue );

jPlayer.volumeMin()

Description
This method is used to mute the the MP3 file being played.
Parameters
none
This method has no parameters.

Code Examples:

$("#jpId").volumeMin();

jPlayer.volumeMax()

Description
This method is used to maximise the volume of the MP3 file being played.
Parameters
none
This method has no parameters.

Code Examples:

$("#jpId").volumeMax();

jPlayer.onSoundComplete( Function: endOfSong )

Description
This method is used to define a function that is executed when the MP3 file finishes playing.
Parameters
endOfSong
Function that is executed when the MP3 file ends.

Code Examples:

$("#jpId").onSoundComplete( function() { $("#jpId").play(); } );
$("#jpId").onSoundComplete( function() { /* Something groovy! */ } );

jPlayer.onProgressChange( Function: handleChanges )

Description
This method is used to define a function that takes 5 parameters and is executed every time the plugin updates the progress of the load bar or play bar.
Parameters
handleChanges( Number: loadPercent, Number: playedPercentRelative,
Number: playedPercentAbsolute, Number: playedTime, Number: totalTime )
Function executed every time the progress changes.
Parameters:
loadPercent
Number (0 to 100) defining the percentage downloaded
playedPercentRelative
Number (0 to 100) defining the percentage played when compared to the percentage downloaded.
playedPercentAbsolute
Number (0 to 100) defining the the current play position in percentage
playedTime
Number defining the current play position in milliseconds
totalTime
Number defining the total play time of the MP3 in milliseconds

Code Examples:

$("#jpId").onProgressChange( function(lp,ppr,ppa,pt,tt) { $("#myLoadInfo").text(lp+"%"); $"#myPlayInfo").text(ppa+"%"); } );
$("#jpId").onProgressChange( function(lp,ppr,ppa,pt,tt) { /* Something groovy! */ } );

jPlayer's CSS Classes

The jPlayer plugin uses a number of classes, defined in the CSS, that allow the plugin to control how jPlayer's artwork is displayed on the webpage. All the classes are optional and their use depends on how you choose to represent the jPlayer graphically. CSS classes are defined in the CSS stylesheet for the webpage, and are valid for IDs declared by the jPlayer.jPlayerId() method.

All classes should be defined as ID specific classes. Eg. #mySpecificId.myClass { ... }

cssPrefix_hover

Description
The cssPrefix_hover class is used for the Mouse Hover state, however you may style the :hover yourself in the CSS if you wish.
This functionality solves a problem in IE7 where the CSS :hover state was not working with <div> tags used to define the artwork areas.
The hover state is available to all id's declared by the jPlayer.jPlayerId() method.
In practice you probably only want to use the functionality for the user inputs on the jPlayer interface you create.
ie., Play, Pause and Stop buttons, the volume buttons and maybe the Load Bar.
Default class: jqjp_hover

Code Examples:

#thePlayButton {
  background: url("images/jplayer_play.gif") no-repeat top left;
}

#thePlayButton.jqjp_hover {
  background: url("images/jplayer_play_hover.gif") no-repeat top left;
}

cssPrefix_buffer

Description
The cssPrefix_buffer class is used for the Load Bar's buffer state.
When the MP3 file is buffering, this class is applied to the id declared by: .jPlayerId("loadBar", "theLoadBar")
Default class: jqjp_buffer

Code Examples:

#theLoadBar {
  background: url("images/jplayer_load_bar.gif") repeat-x top left;
}

#theLoadBar.jqjp_buffer {
  background: url("images/jplayer_load_bar_buffer.gif") repeat-x top left;
}

jPlayer Compatibility

jQuery

Compatibility verified with:

  • jQuery 1.3.2
  • jQuery 1.3.1
  • jQuery 1.3
  • jQuery 1.2.6

Browser

Compatibility verified with:

  • Internet Explorer 6
  • Internet Explorer 7
  • Internet Explorer 8
  • Firefox 3
  • Safari 3
  • Google Chrome 1
  • Opera 9
    • Progress bar steps are quantised to 1% increments.