...

View Full Version : NiftyPlayer, how to cause an action when the song is over?



DaveLinger
12-10-2010, 01:37 AM
Hi!

I'm using NiftyPlayer (http://www.varal.org/niftyplayer/) to play an mp3 on my website. I'd like it to refresh the page when the song is over (I have some php grabbing songs from a database and choosing the one to play at random).

NiftyPlayer has two functions that are probably useful for this. First, if you have a link go to:


javascript:alert(niftyplayer('niftyPlayer1').getState())

It will display the current state of the player (stopped, playing, paused, finished). Presumably, a piece of javascript could poll this variable every second, and once it equals "finished", execute the redirect.

However, I think this other piece of code included with NiftyPlayer would help even more. In the .js file, it says the following:


this.registerEvent = function (eventName, action) {
// eventName is a string with one of the following values: onPlay, onStop, onPause, onError, onSongOver, onBufferingComplete, onBufferingStarted
// action is a string with the javascript code to run.
//
// example: niftyplayer('niftyPlayer1').registerEvent('onPlay', 'alert("playing!")');

this.obj.SetVariable(eventName, action);
};

So it would appear that I could use something like this to automatically refresh when the song is over:


niftyplayer('niftyPlayer1').registerEvent('onSongOver', 'location.reload(true)');

The problem here is that I have no javascript experience and have no idea how to implement either of these ideas. Help?

Philip M
12-10-2010, 09:43 AM
Try this:-


this.registerEvent = function(eventName,action) {
niftyplayer('niftyPlayer1').registerEvent('onSongOver', 'location.reload(true)');
this.obj.SetVariable(eventName, action);
}

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

DaveLinger
12-10-2010, 06:00 PM
Philip,

Thanks for your reply. I replaced my block of code with the one you supplied, but still nothing happens when the song is done. Do I need any code on the html page to call the javascript other than this in the head?


<script type="text/javascript" language="javascript" src="niftyplayer.js"></script>

(niftyplayer.js contains the code I posted)

Philip M
12-10-2010, 06:58 PM
language="javascript" is obsolete and deprecated, so you should remove it.

For the rest, I have no experience of niftyplayer so my suggestions are just (educated) guesses. I do not see why it should not work if you replaced the code with the code I gave you, but perhaps there are other problems.

DaveLinger
12-10-2010, 07:01 PM
Philip,

Thanks again for your help.

What I ended up doing was using php to determine the length of the currently-playing mp3, and using setTimeout to refresh after that length plus 2 seconds. It works, but doesn't take into account any stopping from buffering.

Philip M
12-10-2010, 07:04 PM
Philip,

Thanks again for your help.

What I ended up doing was using php to determine the length of the currently-playing mp3, and using setTimeout to refresh after that length plus 2 seconds. It works, but doesn't take into account any stopping from buffering.

OK, but it does sound a bit of a hack. Did you try anything with
javascript:alert(niftyplayer('niftyPlayer1').getState())
to try and see what is happening?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum