Nathaniel S
Nathaniel S

Reputation: 335

Super easy audio player with Flash + jQuery

I am trying to make an super easy audio player with jQuery.

How would you convert this

   <a href="song.mp3">Song</a>

to this?

   <object type="application/x-shockwave-flash" data="dewplayer.swf" >
        <param name="flashvars" value="mp3=blank.mp3" />
   </object>

   <a href="song.mp3">Download</a>

So what needs to happen as I understand it

This utilizes a simple flash audio player, Dewplayer.

Any thoughts?

Upvotes: 0

Views: 1342

Answers (1)

Matt Ball
Matt Ball

Reputation: 359836

Like this (untested):

// on document ready,
$(function ()
{
    var objTagOpen = '<object type="application/x-shockwave-flash"'
                     + ' data="dewplayer.swf">'
                     + '<param name="flashvars" value="mp3=',
        objTagClose = '" /> </object>';

    // find all the links whose href ends with '.mp3',
    // and for each one,
    $('a[href$=.mp3]').each(function ()
    {
        var $this = $(this);

        // insert the flash <object> with the flashvars parameter
        $this.before(objTagOpen + $this.attr('href') + objTagClose);

        // then rewrite the link itself
        $this.text('Download');
    });
});

super-easy indeed.


Edit: Pekka's absolutely right about using a rel to allow you to have normal mp3 links as well. In that case, all you need to do is rewrite your initial selector, from

$('a[href$=.mp3]')

to

$('a[href$=.mp3][rel=mp3]')

Upvotes: 2

Related Questions