Link to home
Start Free TrialLog in
Avatar of s1desh0w
s1desh0w

asked on

javascript cross browser element array like getElementsByTagName

Hi,

I am trying to provide a number of audio files for streamin playback, the files are .swf. Since there will be multiple <EMBED> tags my 1st problem was that all tracks could be played at the same time, and despite setting the autostart property to false all of the embedded sounds played on page load. I read that to prevent this to use the following javascript in body onload: document.elementid.rewind(). This indeed worked and nothing plays now till document.elementid.play() is called.

Now, since the embed tags are dynamically generated there could be any number of them, i needed a way to stop any playing track before starting another and i wrote these functions:

function stop_all()
      {
      //stop any other tracks
      var arrEmbedArray=document.getElementsByTagName("embed");
      for(var i=0;i<arrEmbedArray.length;i++)
            {
            arrEmbedArray[i].rewind()
            alert(i)
            }
      }

function start_track(strEmbedID)
      {
      stop_all()
      //start selected track
      var objCurrentEmbed=document.getElementById(strEmbedID);
      objCurrentEmbed.play()
      }

Really i wanted an "if" in the loopin stop_all() so the rewind is only called for the one playing track but i couldn't find a way so it is being called for each and every embed element, i'd like to change this but it works... Well, it's working in IE, but not in netscape, firefox, or opera. The alert in the loop was to test the loop was running and it isnt running in any other browser than IE.

I read that getElementsByTagName is supported as follows: IE v5 +, Firefox v1 + and opera v9 +. I think i may need to do things differently for these browsers, hope someone can help! thanks all.

Avatar of DireOrbAnt
DireOrbAnt

Why don't you keep the current track and change like this:

var CurrentEmbedID = '';

function stop_track()
      {
       if (CurrentEmbedID != '')
        {
         document.getElementById(CurrentEmbedID).rewind();
         CurrentEmbedID = '';
        }
      }

function start_track(strEmbedID)
      {
      stop_track();
      //start selected track
      CurrentEmbedID = strEmbedID;
      document.getElementById(CurrentEmbedID).play()
      }
Avatar of s1desh0w

ASKER

yes, thanks, i did think of keeping the current track as a global var after making the post, i havnt tried it yet, i will let you know if that works, more importantly will this work cross browser because there is some problem in all browsers except ie that is preventing the code in my for loop from running (getElementsByTagName). In addition all tracks will play onload unless i call .rewind() for each one onload as well, thats what stop_all() is doing, so i should keep this function as is?

thanks for your help
ASKER CERTIFIED SOLUTION
Avatar of DireOrbAnt
DireOrbAnt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i've used a workaround to get round this problem by using the following javescript to write the flash object to a <div> from an onclick event: It saves having to stop the tracks on load and on play.

function start_track(strEmbedSource)
      {
      objLayer=document.getElementById("obj_layer")
      objLayer.innerHTML='<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" WIDTH=0 HEIGHT=0><PARAM NAME=movie VALUE="' + strEmbedSource + '" /><PARAM NAME=loop VALUE=false /><PARAM NAME=volume VALUE=100 /><PARAM NAME=quality VALUE=high /><EMBED src="' + strEmbedSource + '" loop=false volume=100 quality=high width=0 height=0 hidden=true type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed></object>'
      }

the only problem i have remaining is that in the same way as setting <param name=autostart value=false> doesnt prevent these files from playing on load, setting <param name=loop value=false> doesnt prevent the track from looping either. as you can see from the code above i have included these values in both the param name and the embed tag, the files by the way are only audio but converted from mp3 to swf at a lower quality to save bandwidth on streaming, i used MP3 Stream trial version to do this, thanks again for ure help.
p.s. i will award the points for your previous answer tho since i believe it would solve my original problem. cheers