Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    NiftyPlayer, how to cause an action when the song is over?

    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:

    Code:
    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:

    Code:
    	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:

    Code:
    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?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Try this:-

    Code:
    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.

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

    Code:
    <script type="text/javascript" language="javascript" src="niftyplayer.js"></script>
    (niftyplayer.js contains the code I posted)

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.

  • #5
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by DaveLinger View Post
    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?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •