Tuesday 24 June 2008

Using Yahoo! Media Player on a dynamic page

Recently I added paging to my churches podcast page, the steps on how I did this are part of another post that I aim to finish at some point, however I thought I'd quickly blog tonight some tips on using the Yahoo! Media Player on a dynamic page.


Originally the podcast paging was causing a full postback which reloaded everything on the page, this felt dirty so tonight I reconfigured my podcast control to make use of the ASP.Net Ajax UpdatePanel. The paging worked great however the WMP wasn't updating which left the playlist out of date and the new podcasts without play buttons.


The issue was that by default the YMP JS fires on load and the ajax update wouldn't trigger this. Moving the script to load as part of the ajax updated content also wouldn't help this, all I would be doing would be reloading the source, not actually solving the problem.


I turned to the YMP wiki however I didn't really turn anything up that was of great use.


I knew that the YMP scraped the page on load for MP3 files and then built up its playlist, so it was a case of finding this scrape function, clearing the current playlist and then triggering the scrape.


Firebug came to my rescue, I add a watch to the YMP object YAHOO.music.WebPlayer and found that it had quite a few functions available if you know they are there, I have listed a few useful ones below:

  • asyncLoadPlayer() - if the player is loaded after the window has loaded this will manually load the whole player
  • clear() - clears the playlist
  • pause() - pauses the item playing in the media player
  • play() - plays the current item in the playlist
  • scrape() - scrapes the page for mp3's and adds them to the playlist
  • shutdown() - shutsdown the media player and releases all resources
  • startup() - starts the player up, not sure if this is useful as asyncLoadPlayer()is probably better
  • stop() - stops the item playing in the media player


So for my case with a dynamically loading page I added some simple JavaScript to the content that is loaded in, this JavaScript first ensures YMP has already been loaded and then clears the playlist and scrapes the page for new media items for adding to the playlist.


function PageLoadedEventHandler() {
  if(YAHOO.music.WebPlayer.loaded){      
    YAHOO.music.WebPlayer.clear();      
    YAHOO.music.WebPlayer.scrape();
  }
}

I did ponder about leaving out the clear, that way as you page through the podcasts the playlist simply grows so you can listen to any of them, I decided against this but I hope you can see what you can do.


So there wasn't much to it, I am hoping Yahoo! will properly document the functions available within the YMP as I would have saved alot of time faffing with FireBug and the watch window. I hope this helps someone, I'm now thinking on how I could use my own player interface and utilise YMP to power it... any way thats something for a rainy day

12 comments:

Lucas Gonze said...

So that people looking on the YMP wiki do turn up useful stuff, I have added links to this post in a couple different places on the wiki.

Unknown said...

The function looks awesome, exactly what I need, however looks like Yahoo change the class.
YAHOO.music.WebPlayer.loaded doesn't exist any more. Now you have to use
YAHOO.mediaplayer.playerAlreadyLoaded
and I can't find the new 'scrape()'. I was wondering if you have an updated version of this function. Thanks

JeanVal said...

I have just come across Yahoo Media Player and it seems quite interesting but is quite baffling. Your article references ‘clear()’ and ‘scrape()’ functions which are invoked with calls such as ‘YAHOO.music.WebPlayer.scrape();’ but the documentation for the 2.0 API at http://mediaplayer.yahoo.com/api/ has no such functions defined. Moreover the API document references the YMP object as ‘YAHOO.MediaPlayer’ not ‘YAHOO.music.WebPlayer’??? The API document also describes a ‘play()’ functions which I cannot get to work although other functions, such as ‘setPlayerViewState()’, ‘next()’, and previous()’, work fine. Any clarification of these issues would be greatly appreciated particularly how to get the ‘play()’ function to play a ‘.mp3’ file via JavaScript.

Michael James said...

Unfortunately since this blog post the api has moved onto version 2, the post was specifically related to version 1. Version 2 sadly has breaking changes due to the api changes / renaming.

I'm yet to look at version 2 properly although I need to as the sites I have used the YMP on I've had to target to use the V1 API.

As and when I get time to look at the v2 API I'll update this post

Lucas Gonze said...

Here's a comment that Rich Schiavi, who worked on the player, emailed to me. This is about the API changes.

Lucas,

Couldn't add a comment for some reason on this guys page: https://www.blogger.com/comment.g?blogID=2153751547788086073&postID=675344154730529941

But here is how you rescrape/ajax with the new YMP 2.0

YAHOO.MediaPlayer.addTracks(document.getElementById("articleEntries",0,false))


"articleEntries" is the id of a div you want to scrape from, so you'd call this perhaps after you ajaxaly updated that div tag...

Rich

Lucas Gonze said...

The post cut off a key bit of the code. Trying again, this time with line breaks:

YAHOO.MediaPlayer.addTracks(
document.getElementById(
"articleEntries",0,false
)
)

Unknown said...

it has well written content on YMP, there are having some error in embedded text file of Yahoo Media Player as I'm checking my site for our web 2.0 development company; me to looking for the suitable informative one to run his proper . .

Anonymous said...

He wholesale ugg boots looked across the sea ugg boots outlet and knew how alone he was now. But he could see the prisms in the deep dark uggs outlet water and the line stretching ahead and the strange undulation of the calm. The clouds were building up now for the trade wind and he looked ahead and nike shox saw a flight of wild ducks ugg outlet etching themselves against the sky over the water, the blurring, then etching again and he knew no man was ever alone on the sea.

Anonymous said...

ugg boots online
ugg online
buy ugg
buy uggs
buy ugg boots
uggs online
american ugg boots

Ugg Boots
Uggs
Ugg Shoes

Ugg Sheepskin Boots
Ugg Australia Boots

pipicoms said...

Thanks Lucas! it works

Unknown said...

Some of our customers have experimented with the Yahoo! Media Player, a new player designed to make it easier to integrate audio files into your site.

When you add the Yahoo! Media Player to your page, this play button will appear next to each of your audio links.
buy wow accounts

Techwiz said...

Die! Spammers, Die!