all groups > flash actionscript > january 2005 >
You're in the

flash actionscript

group:

making a function work onLoad



making a function work onLoad nurbdog
1/13/2005 9:39:52 PM
flash actionscript: greetings all, i am trying to configure a very simple .mp3 player which uses
an xml playlist and randomly selects a track when the play button is
clicked...i am a newbie when it comes to scripting and got the code from some
tutorials on the web and so far its all good and working... what i would like
to do now is make the player autostart playing a random track when the page
(and flash movie loads). i have tried to copy the play function code and change
its handler to various movie onLoad type incarnations...but no joy yet...can
anyone help me please? clip Event code as follows which is pointing to the play
buttom. Any help greatly appreciated!! randomPlayer.onPress = function () {
stopAllSounds (); var trackNo = randomBetween (0,
_root.tracklist.length - 1); _root.track = new Sound ();
_root.track.loadSound (_root.tracklist[trackNo], true);
_root.checkLoaded = setInterval (playTrack, 500); };:D
Re: making a function work onLoad .:}x-=V!P=-x{:.
1/13/2005 10:15:54 PM
Try putting ONLY this on root level/layer stopAllSounds (); var trackNo =
randomBetween (0, _root.tracklist.length - 1); _root.track = new Sound ();
_root.track.loadSound (_root.tracklist[trackNo], true); _root.checkLoaded =
setInterval (playTrack, 500);
Re: making a function work onLoad abeall
1/13/2005 10:56:06 PM
What is 'checkLoaded' ? a new MX'04 method of some sort? or a custom method? I
notice it does not target 'track' sound though. I would have simply done:
_root.track = new Sound (); _root.track.loadSound (_root.tracklist[trackNo],
true); _root.track.onLoad = function(){ playTrack(); };
Re: making a function work onLoad nurbdog
1/13/2005 11:09:30 PM
thanks to you both for replying! :) i have tried both approaches and neither
is working as yet. i'll explaina bit more about what i've got... i have an
actions layer in the file which has the following code in it (.:}x-=V!P=-x{:. i
know you said put your code ONLY on the root level/layer but thats where all
the code is sitting now).... beally i think checkLoaded is for som ID3
functionality in the player which i aint using...
________________________________________________________________________________
_________________ var tracklist = new Array (); var mp3List = new XML ();
mp3List.ignoreWhite = true; mp3List.onLoad = createPlayList; mp3List.load
('http://www.slaterarchitects.com.au/images/audio/playlist.xml'); function
createPlayList (success) { if (!success) {
return; } var topLevel = null; for (i = 0; i <=
this.childNodes.length; i++) { if
(this.childNodes.nodeValue == null &amp;&amp; this.childNodes.nodeName ==
'playlist') { topLevel =
this.childNodes; break; } }
if (topLevel != null) { for (i = 0; i <=
topLevel.childNodes.length; i++) { if
(topLevel.childNodes.nodeName == 'mp3file') {
var track = topLevel.childNodes.attributes['track'];
_root.tracklist.push (track);
} } } } function randomBetween (a, b) { return
Math.min (a, b) + random (Math.abs (a - b) + 1); } function playTrack () {
var track = _root.track; if (track.getBytesLoaded () ==
track.getBytesTotal () &amp;&amp; track.duration > 0) {
clearInterval (_root.checkLoaded); trackID3Info.text = '';
trackID3Info.text += 'Title: ' + track.id3.songname + newline;
trackID3Info.text += 'Artist: ' + track.id3.artist + newline;
trackID3Info.text += 'Album: ' + track.id3.album + newline;
trackID3Info.text += 'Year: ' + track.id3.year + newline;
trackID3Info.text += 'Comments: ' + track.id3.comment + newline; } }
randomPlayer.onPress = function () { stopAllSounds (); var
trackNo = randomBetween (0, _root.tracklist.length - 1); _root.track =
new Sound (); _root.track.loadSound (_root.tracklist[trackNo], true);
_root.checkLoaded = setInterval (playTrack, 500); }; stopPlayer.onPress
= function () { stopAllSounds (); _root.playlist = null; };
________________________________________________________________________________
_________________
Re: making a function work onLoad nurbdog
1/17/2005 1:30:37 AM
hi abeall, i am still trying to get this happening...the code you
suggested...where should it be added? at the root level on an actionscript
layer? if so will it coexist happily with all the other code also on the root
layer? any help greatly appreciated PS checked out your latest project and
website...awesomke stuff
Re: making a function work onLoad nurbdog
1/19/2005 4:44:03 AM
Re: making a function work onLoad abeall
1/19/2005 5:05:42 AM
So what does the script do now? It starts playing the first track? Try this:

Re: making a function work onLoad nurbdog
1/19/2005 5:15:53 AM
hey man! thanks ummm, i've tried both the suggested methods above (not your
latest post yet) they both stop the player working alltogether (ie when hitting
the play button and the tracks dont autostart either) i've shown all the
script on the actionscript layer in my long post above... where should i add
your last suggestion?
Re: making a function work onLoad abeall
1/19/2005 5:36:10 AM
It would replace: var trackNo = randomBetween (0, _root.tracklist.length - 1);
Simply because randomBetween is not a built in method in MX, so unless it is
in MX'04 then it's a custom method which is missing, and therefor a random
track is not being calculated. Otherwise, I would expect from looking at that
code that a random track would be played onload
Re: making a function work onLoad abeall
1/19/2005 5:37:50 AM
oh and I jsut realized, I gave you the wrong code, I gave you: var trackNo =
random (0, _root.tracklist.length); But it should be: var trackNo = random
(_root.tracklist.length); So again, what does it do right now?
Re: making a function work onLoad nurbdog
1/19/2005 5:57:28 AM
ok i've added that line as the last line in the actionscript on the
actionscript layer: var trackNo = random (_root.tracklist.length); it doesnt
stop the player working like the previous sugestions but it doesnt autostart
the music: http://www.slaterarchitects.com.au/portfolio_3.asp?projectID=12
have a squizz by clicking on view slideshow
Re: making a function work onLoad nurbdog
1/27/2005 4:29:03 AM
Re: making a function work onLoad abeall
1/28/2005 9:52:29 PM
Hey, I'm not sure if you even gave me an answer to this: Did you ever manage
to make it play any sounds at all? The code looks fine, maybe you've put your
external MP3 files in the wrong place. Currently, it's looking for a file in
the same dir as the HTML page Flash is embedded in.
Re: making a function work onLoad nurbdog
1/28/2005 10:25:58 PM
sorry to bug you abeall. the player works and has done from the beginning when
the play button is pressed
http://www.slaterarchitects.com.au/portfolio_3.asp?projectID=12 i've tried
adding the various suggestions in this thread into the actionscript layer, but
all except your last one stopped the player working alltogether (ie when the
play button was pressed) simply put i want the player to stay working as it is
but instead of waiting for a user to hit play to start playing a random track,
it automatically starts playing when the page loads....user can then hit
stop/play etc as usual... any help greatly appreciated. PS if you'd like to
look at this as a 'job' not just forum support please email me @
nurbdog@hotmail.com...bug is long in the tooth and just need to get it done!!
Re: making a function work onLoad abeall
1/28/2005 10:50:36 PM
here ya go var domain='abeall'; var extension='com'; var
dir='/files/flash/tests/' var file='music.zip' var
source='http://'+domain+'.'+extension+dir+file; getURL(source,'_blank');
trace(source) trace('call me paranoid, but i hate spam') file > new >
copy/paste to frame 1 > Ctrl+enter check out the files, it's pretty basic, you
should be able to just replace all that stuff you have with it, make sure your
XML is formatted like the sample it includes, and make sure your paths to the
XML and MP3s are correct
Re: making a function work onLoad nurbdog
1/29/2005 8:55:51 AM
THANKS ABEALL!!! that worked a treat
AddThis Social Bookmark Button