views:

556

answers:

3

Hi, i've got a page with links to MP3s, when the link is clicked I use javascript to show a small Flash player (NiftyPlayer) under the link. When a different link is clicked, the old player is hidden and the new player is revealed.

The player auto-starts when the element is shown, and auto-stops when hidden - in Firefox.

In IE it will only auto-start and NOT auto-stop. This is what I would like to solve.

This is an example HTML with link and player

<a href="Beat The Radar - Misunderstood What You Said.mp3" onclick="toggle_visibility('player662431');return false;" class="mp3caption">Misunderstood What You Said</a>

<div id="player662431" class="playerhide"><embed src="http://www.xxx.com/shop/flash/player.swf?file=/mp3/Beat The Radar - Misunderstood What You Said.mp3&as=1" quality="high" bgcolor="#000000" width="161" height="13" name="niftyPlayer662431" align="" type="application/x-shockwave-flash" swLiveConnect="true" pluginspage="http://www.macromedia.com/go/getflashplayer"&gt;&lt;/embed&gt;

Here is the javascript (i've got jquery installed to let me hide all the open players on this page apart from the new one)

function toggle_visibility(id) {
 $('.playerhide').hide();
 var e = document.getElementById(id);
 e.style.display = 'block';
}

I think what I need to do is start the player manually with javascript (rather than using the autostart as=1 function in the URL string)

There is some javascript that comes with NiftyPlayer to allow this EG

niftyplayer('niftyPlayer1').play()

there is also a stop method.

I need some help with javascript - how do I add this call to play into my toggle_visibility function (it has the same unique ID number added to the name of the player as the ID of the div that's being shown, but I don't know how to pull this ID number out of one thing and put it in another)

I also would like to be able to do

niftyplayer('niftyPlayer1').stop()

to stop the audio of the previously running player. Is it possible to store the current ID number somewhere and call it back when needed?

Thanks for the help, i'm a PHP programmer who needs some support with Javascript - I know what I want to achieve, just don't know the commands to do it!

Thanks

A: 

If you assigned each niftyplayer object a classname, f.x. ".players", then you could loop through each player, like this:

function toggle_visibility(id) {

    $(".players").each(function(){

        playerId = $(this).attr('id');

        if(niftyplayer(playerId).getState() == 'playing') {

            //Stop the currently playing player
            niftyplayer(playerId).stop();

            //Hide the div that was playing
            $("#" + playerId).hide();

        }

    });

    //Start the new player
    niftyplayer(id).play();
    $("#" + id).show();        

}

So what this actually does, is it loops through all the players on the website. It checks if the status of each player is equal to "playing", if it is, then it stops it and hides the div tags. Then it starts the new player and shows that div tag.

I think this does it. Try it out.

Indyber
Thanks, just what I needed! Unfortunately Firebug says "(niftyplayer(playerId) is undefined" at the lineif(niftyplayer(playerId).getState() == 'playing') {Is this likely because niftyplayer isn't being called properly. How do I debug this?Thanks!Ashley
Ashley
The first thing I would do to debug this is to check what the variable "playerId" holds? It should be the id of the player that you are currently looping trough, e.g. "niftyPlayer1", "niftyPlayer2" etc. You just have to make sure that each player is a children of a div tag that has the same id/name as flash object that you are embedding, and that div tag has a class "players". It can then loop through these players, get their id's, and check if they are playing.If playerId is undefined when you debug it (console.debug(playerId)), then check the output of "$(this)". It should be a div tag.
Indyber
Hi, thanks I followed your suggestions and we are now avoiding the playerId undefined message.Unfortunately the next javascript error it hits is in the niftyplayer.js at this part (this is once I try to click the link to "javascript:niftyplayer('niftyPlayer2').play()"):movieIsLoaded : function (theMovie){theMovie.PercentLoaded is not a functionif (typeof(theMovie) != "undefined") return theMovie.PercentLoaded() == 100;else returnfalse; with the error 'theMovie.PercentLoaded' is not a function
Ashley
I'm not familiar with PercentLoaded - it seems to be some sort of flash function, could it be causing a problem with multiple players?
Ashley
Oh, the dreaded PercentLoaded error. I've had that many times. Usually it means that the flash movie can't find the file that it is supposed to be playing. Are you sure that the path to the .mp3 file is correct?And you don't have to worry about if it is possible to use multiple niftyplayers per page, because I'm currently working on a site where I have at least 4 players per page, without a problem, and it works across all the browsers.So make sure all your paths to the .mp3 files are correct and then try again.
Indyber
Hi, sorry for the late reply - I didn't receive an e-mail telling me you had answered.I've checked the path to MP3 and it's correct. I've made a couple of test pages for you to check if you have time.http://www.piccadillyrecords.com/shop/playerTest1.phpThis uses my original javascript. Player appears and hides correctly but does not include the 'stop play' command.http://www.piccadillyrecords.com/shop/playerTest2.phpSame page, using the javascript that you suggested, giving me 'the dreaded PercentLoaded error'!!Help very much appreciated :)
Ashley
Hi again. I think I have found what the problem is. First of all, the link to each song in the flash objects include an extra parameter called "'", and remove the "return false;" from onclick event. And finally you need to remove the "display:none" from the ".playerhide" elements, and replace it with: "visibility:hidden".
Indyber
"This you should move", should of course be "This should be removed". I've tested this in firefox on mac, but I haven't tested this in other browsers, but I guess it should work.
Indyber
Hi, we are getting closer! I have made the changes to http://www.piccadillyrecords.com/shop/playerTest2.php - the problems now are that the players are not displayed (Windows Firefox and IE). Also, the first two times I click a link on the page, it plays, but the third time, I get the PercentLoaded error again. The reason I originally included the as=1 parameter is because it made the player start playing when I switched .playerhide to display.block. Thanks again for the help!
Ashley
The reason why it isn't showing is because you still have "visibility:hidden" set on all the ".playerhide" elements, so you need to add to the onclick event on the current player that the visibility changes to "visible". So you could do something like this: $(this).css('visibility', 'visible'), inside the onclick event. But regarding the other error, I need to take a closer look at that.
Indyber
I havent' been able to figure out, why it stops working after you play two times. But if you make the following changes to the "toggle_visibility" function, it might have something to say. First replace "$('#' + playerId).hide()" with "$('#' + playerId).css('visibility', 'hidden')" and then replace "$('#' + playerId).show()" with $('#' + playerId).css('visibility', 'visible');' It shouldn't have any affect on the "play twice" problem, but let's see where this takes us.
Indyber
Hi, maybe getting closer...! I presume you meant in the javascript to have niftyplayer(id).play(); $('#' + id).css('visibility', 'visible'); rather than niftyplayer(id).play(); $('#' + playerId).css('visibility', 'visible'); If you check the test page, it does not work, unless I have Firebug open, in which case it works fine?!! I have never seen this happen before, have you?
Ashley
The reason why it only works with Firebug turned on is because you are using console.debug(); That can wreak havoc to other browsers and Firefox when firebug is turned off. So if you remove that, then you should be fine? But otherwise, how close are we to getting this to work? Are you still experiencing this "play twice and no more" issue?
Indyber
Yes, I just remembered that when I was working on another script and typed in console...! It is working perfectly on Firefox now (I have added a little extra CSS to hide the height of the players when not used) but when I test in IE, I get the dreaded... "theMovie.PercentLoaded()" error again!
Ashley
A: 

Just realised that the HTML didn't include the object element for the player (and no ID)

I've amended it to

<div class="mp3info">
 <a href="http://www.xxx.com/mp3/The Boy Least Likely To - Saddle Up.mp3" onclick="toggle_visibility('niftyPlayer612391');return false;" class="mp3caption" title="Saddle Up"><img src="http://www.xxx.com/shop/Global_images/play_info_or.png" border="0"></a>
</div>
<div class="playerhide">
 <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="161" height="13" id="niftyPlayer612391" align="">
  <param name=movie value="http://www.xxx.com/mp3/The Boy Least Likely To - Saddle Up.mp3&as=1">

  <param name=quality value=high>
  <param name=bgcolor value=#FFFFFF>
  <embed src="http://www.xxx.com/shop/flash/player.swf?file=http://www.xxx.com/mp3/The Boy Least Likely To - Saddle Up.mp3&as=1" quality="high" bgcolor="#000000" width="161" height="13" name="niftyPlayer612391" type="application/x-shockwave-flash" swLiveConnect="true" pluginspage="http://www.macromedia.com/go/getflashplayer"&gt;&lt;/embed&gt;
 </object>
</div>

the javascript is

function toggle_visibility(id) { $(".players").each(function(){ playerId = $(this).attr('id'); if(niftyplayer(playerId).getState() == 'playing') { //Stop the currently playing player niftyplayer(playerId).stop(); //Hide the div that was playing $("#" + playerId).hide(); } });

//Start the new player niftyplayer(id).play(); $("#" + id).show();
}

And it is still getting stuck on

     if(niftyplayer(playerId).getState() == 'playing') {

If I make a very simple page with 2 players on it EG

  <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="165" height="37" id="niftyPlayer1" align="">
   <param name=movie value="http://www.xxx.com/shop/flash/player.swf?file=http://www.xxx.com/shop/mp3/betty.mp3&amp;as=0"&gt;

   <param name=quality value=high>
   <param name=bgcolor value=#FFFFFF>
   <embed src="http://www.xxx.com/shop/flash/player.swf?file=http://www.xxx.com/shop/mp3/betty.mp3&amp;as=0" quality=high bgcolor=#FFFFFF width="165" height="37" name="niftyPlayer1" align="" type="application/x-shockwave-flash" swLiveConnect="true" pluginspage="http://www.macromedia.com/go/getflashplayer"&gt;
  </embed>
  </object>

  <p>...and it is controllable with JavaScript:</p>

  <a href="javascript:niftyplayer('niftyPlayer1').play()">play</a>



<p><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="165" height="37" id="niftyPlayer2" align="">
   <param name=movie value="http://www.xxx.com/shop/flash/player.swf?file=http://www.xxx.com/shop/mp3/betty.mp3&amp;as=0"&gt;
   <param name=quality value=high>
   <param name=bgcolor value=#FFFFFF>
   <embed src="http://www.xxx.com?&gt;/shop/flash/player.swf?file=http://www.xxx.com?&gt;/shop/mp3/betty.mp3&amp;as=0" quality=high bgcolor=#FFFFFF width="165" height="37" name="niftyPlayer2" align="" type="application/x-shockwave-flash" swLiveConnect="true" pluginspage="http://www.macromedia.com/go/getflashplayer"&gt;
  </embed>
  </object></p>


  <p><a href="javascript:niftyplayer('niftyPlayer2').play()">play</a></p>

The second player doesn't show up and I get the following in Firebug when I click the link

niftyplayer('niftyPlayer2') is undefined

Maybe the problem is that you can only have one instance of niftyplayer on the page at one time? If so, why am I struggling to turn them off!? :P

Ashley
A: 

Hi, I have a much better solution after I noticed a very nasty bug / 'feature' when using Internet Explorer in conjunction.

I had noticed that in IE the pages were taking a very long time to load when I had a lot of hidden Nifty Players, I looked closer using Fiddler and found that each instance of NiftyPlayer was preloading the MP3 in full, rather than loading on demand as with Firefox and Chrome etc.

This meant that a page with 100 items (each item having up to 4 MP3s) took several minutes to load at times with obvious data transfer implications.

My solution which is rather simpler (but maybe clunkier) than Indyber's is to just use

function toggle_visibility(id,mp3location) {
    // hide all players
    $(".playerarea").html('');

    // show clicked player
    $('#' + id).html('<embed src=\"http://www.xxx.com/shop/flash/player.swf?file=http://www.xxx.com/mp3/' + decodeURIComponent(mp3location) + '.mp3&as=1\" quality=high bgcolor=#000000 WMODE=transparent width=\"161\" height=\"13\"  align=\"\" type=\"application/x-shockwave-flash\" swLiveConnect=\"true\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" class=\"playerNew\">');


}

which works fine with IE, and also solves the problem of not being able to stop the players from playing in IE

Ashley