WordPress audio player with jquery
How to Use WordPress audio player (standalone version) with jquery and jquery swfobject (progressive enhancement ).
<! -- Section "WordPress audio player with jquery" [1-272] -->
Example 1: Basic
View demo
Html
Java code
- <A class = "audio" href = "audio/reason.pdf">
- Audio: An electronic reason
- </A>
<a class="audio" href="audio/reason.mp3">Audio: An Electronic Reason</a>
Javascript
<! -- Section "Example 1: Basic" [273-705] -->
Java code
- $ ('. Audio'). Each (function (){
- Audio_file = $ (this). ATTR ('href ');
- $ (This). Flash (
- {
- SWF: 'Flash/audioplayer.swf ',
- Flashvars:
- {
- Soundfile: audio_file
- }
- }
- );
- });
$('.audio').each(function(){audio_file = $(this).attr('href'); $(this).flash({swf: 'flash/audioplayer.swf',flashvars:{soundFile: audio_file}});});
Example 2: Several synchronized players
View demo
Javascript
<! -- Section "Example 2: Several synchronized players" [706-1488] -->
Java code
- // Close other audio players
- // Called by audio player when click on PLAY button
- Function ap_stopall (player_id)
- {
- $ ('. Audio'). Each (function (){
- If ($ (this). ATTR ('href ')! = Player_id)
- {
- $ (This). Find ('object') [0]. setvariable ("closeplayer", 1 );
- }
- Else
- {
- $ (This). Find ('object') [0]. setvariable ("closeplayer", 0 );
- }
- });
- }
- $ (Document). Ready (function (){
- $ ('. Audio'). Each (function (){
- Audio_file = $ (this). ATTR ('href ');
- $ (This). Flash (
- {
- SWF: 'Flash/audioplayer.swf ',
- Flashvars:
- {
- Playerid: "'" + audio_file + "'",
- Soundfile: audio_file
- }
- }
- );
- });
- });
// close other audio players// called by audio player when click on play button function ap_stopAll(player_id){$('.audio').each(function(){if($(this).attr('href') != player_id){$(this).find('object')[0].SetVariable("closePlayer", 1);}else {$(this).find('object')[0].SetVariable("closePlayer", 0);}});} $(document).ready(function() {$('.audio').each(function(){ audio_file = $(this).attr('href'); $(this).flash({swf: 'flash/audioplayer.swf',flashvars:{ playerID: "'" + audio_file + "'", soundFile: audio_file}});});});
Notes
How it works:
- Players are given an ID upon Initialization
- When click on PLAY button, player CILS
ap_stopAll()
With its ID as parameter
- Ap_stopall (): stops all players but the one with this ID
- The ID here is the audio file path, but anything else is possible.
<! -- Section "Notes" [1489-1786] -->
Example 3: real world
View demo
Html
Java code
- <P>
- <A class = "audio" href = "audio/reason.pdf" id = "reason">
- Audio: An electronic reason
- </A>
- </P>
- <P>
- <A class = "audio" href = "audio/sunday.pdf" id = "Sunday">
- Audio: By Sunday afternoon
- </A>
- </P>
<p><a class="audio" href="audio/reason.mp3" id="reason">Audio: An Electronic Reason</a> </p> <p><a class="audio" href="audio/sunday.mp3" id="sunday">Audio: By Sunday Afternoon</a></p>
Javascript
Java code
- // Close other audio players
- // Called by audio player when click on PLAY button
- Function ap_stopall (player_id)
- {
- $ ('. Audio_flash'). Each (function (){
- If ($ (this). ATTR ('id ')! = Player_id)
- {
- $ (This). Find ('object') [0]. setvariable ("closeplayer", 1 );
- }
- Else
- {
- $ (This). Find ('object') [0]. setvariable ("closeplayer", 0 );
- }
- });
- }
- $ (Document). Ready (function (){
- $ ('. Audio'). Each (function (){
- Audio_file = $ (this). ATTR ('href ');
- Audio_title = $ (this). Text ();
- Audio_id = $ (this). ATTR ('id ');
- DIV = $ ('<Div class = "audio_flash" id = "' + audio_id + '"> </div> ');
- $ (This). After (DIV );
- $ (This). After (audio_title );
- $ (This). Remove ();
- Div. Flash (
- {
- SWF: 'Flash/audioplayer.swf ',
- Flashvars:
- {
- Soundfile: audio_file,
- Playerid: "'" + audio_id + "'",
- Quality: 'high ',
- Lefticon: '0xffffff ',
- Righticon: '0xffffff ',
- Leftbg: '0x357cce ',
- Rightbg: '0x32bd63 ',
- Rightbghover: '0x2c9d54 ',
- Wmode: 'transparent'
- },
- Height: 50
- }
- );
- });
- });
// close other audio players// called by audio player when click on play button function ap_stopAll(player_id){$('.audio_flash').each(function(){if($(this).attr('id') != player_id){$(this).find('object')[0].SetVariable("closePlayer", 1);}else {$(this).find('object')[0].SetVariable("closePlayer", 0);}});} $(document).ready(function() { $('.audio').each(function() {audio_file = $(this).attr('href'); audio_title = $(this).text();audio_id = $(this).attr('id'); div = $('<div class="audio_flash" id="' + audio_id + '"></div>');$(this).after(div);$(this).after(audio_title);$(this).remove();div.flash({swf: 'flash/audioplayer.swf',flashvars:{soundFile: audio_file,playerID: "'" + audio_id + "'",quality: 'high',lefticon: '0xFFFFFF',righticon: '0xFFFFFF',leftbg: '0x357CCE',rightbg: '0x32BD63',rightbghover: '0x2C9D54',wmode: 'transparent'},height: 50});}); });
<! -- Section "Example 3: real world" [1787-3238] -->
Notes
- Meaningful HTML: visitors without JavaScript get a download link, otherwise it's replaced by plain text and the player
- The appearance can be customized with customized options (bottom of the page ).
- The default white space before and after the player is already ced by the "height" Flash parameter.
- Use of a custom ID (set on the HTML link)
<! -- Section "Notes" [3239-3682] -->
Download
Download all examples (ZIP, 1.4 MB)
<! -- Section "download" [3683-] -->