Jump to content


Stumped trying to add Time display...


  • You cannot reply to this topic
2 replies to this topic

#1 brittany75

    Young Padawan

  • Members
  • Pip
  • 3 posts

Posted 17 July 2006 - 04:11 PM

I got to the second part of the MP3 Player with XML tutorial (thank god, i'm still a newbie at flash :blink:) and all I want to do is add the time display, i don't want the volume control and all that other stuff. I just can't get it to work and i'm confused on where I should put the codes. Here's my actionscript without the time display stuff:

Quote

stop();
playlist = new XML();
playlist.ignoreWhite = true;
playlist.onLoad = function(success) {
if (success) {
_global.songname = [];
_global.songband = [];
_global.songfile = [];
for (var i = 0; i<playlist.firstChild.childNodes.length; i++) {
_global.songname[i] = playlist.firstChild.childNodes[i].attributes.name;
_global.songfile[i] = playlist.firstChild.childNodes[i].attributes.file;
trace(songname[i]+" "+songfile[i]);
}
}
_root.createEmptyMovieClip("sound_mc", 1);
_root.sound_mc.sound_obj = new Sound();
_global.song_nr = random(songfile.length);
_root.sound_mc.songStarter(songfile[song_nr], songname[song_nr]);
};
MovieClip.prototype.songStarter = function(file, name) {
this.sound_obj.loadSound(file, true);
this.onEnterFrame = function() {
if (this.sound_obj.position>0) {
delete this.onEnterFrame;
this._parent.display_txt.text = name;
} else {
this._parent.display_txt.text = "loading...";
}
};
this.sound_obj.onSoundComplete = function() {
(song_nr == songfiles.length-1) ? _global.song_nr=0 : _global.song_nr++;
_root.sound_mc.songStarter(songfiles[song_nr], songname[song_nr]);
};
};
btn_play.onRelease = function() {
this._parent.sound_mc.songStarter(songfile[song_nr], songname[song_nr]);
};
btn_stop.onRelease = function() {
this._parent.sound_mc.sound_obj.stop();
};
btn_prev.onRelease = function() {
(song_nr == songfile.length-1) ? _global.song_nr=0 : _global.song_nr++;
_root.sound_mc.songStarter(songfile[song_nr], songname[song_nr]);
};
btn_next.onRelease = function() {
(song_nr == 0) ? _global.song_nr=songfile.length-1 : _global.song_nr--;
_root.sound_mc.songStarter(songfile[song_nr], songname[song_nr]);
};
playlist.load("playlist.xml");

Can anybody help me?

#2 funkysoul

    The Funky Stuff

  • Publishing Betazoids
  • PipPipPipPip
  • 2,307 posts
  • Gender:Male
  • Location:Zurich, Switzerland
  • Interests:Music: HIM, HIM, HIM, Cafe del Mar, Linkin Park, Fort Minor, Coldplay, Eric Jordan<br />Sports: Snowboarding, KiteSurfing, Extreme Sports<br />Computer: Flash, After Effects, Actionscript

Posted 17 July 2006 - 05:13 PM

I thought I've explained pretty good, oh well... here you go ;)

First of all create a dynamic textfield, if you using flash 8 don't forget to embed the font by clicking on the embed button and choosing basic latin, give also the newly create textfield an instance name of "timeDisplay" without the quotes

here comes the code, I've commented it so you can see what every line does :D

function timer(sound_obj) { //function with the name "timer" pointing to the sound_obj created earlier
	time = sound_obj.position/1000; //setting a variable called time and dividing the position of the sound by 1000, flash uses ms
	min = Math.floor(time/60); //variable min is calculated from time divided by 60 and rounded to a full number
	min = (min<10) ? "0"+min : min; // very simple, if minutes less then 10 then add a 0 to the min variable
	sec = Math.floor(time%60); // the rest that stays left over from the time calculation are the seconds thats why we use the modulo operator (%)
	sec = (sec<10) ? "0"+sec : sec; // same like min
	timeDisplay_txt.text = min+":"+sec+"/"+totalDuration; assign to the textfield minutes, seconds as also the totalTime
}
//as you see the duration function does the same like the function before, but we need again to calculate the duration in a more readable format then milliseconds

function duration() { 
	timed = _root.sound_mc.sound_obj.duration/1000; 
	mind = Math.floor(timed/60);
	mind = (mind<10) ? "0"+mind : mind;
	secd = Math.floor(timed%60);
	secd = (secd<10) ? "0"+secd : secd;
	totalDuration = mind+":"+secd;
}

When all this is done, we need to call up the duration function every second so we're sure that the timing is being updated correctly.

on the last line of the code add this:
setInterval(duration, 100);// this little helper calls the function every 100ms, you can also put it higher if you want

GL
funkysoul

#3 brittany75

    Young Padawan

  • Members
  • Pip
  • 3 posts

Posted 17 July 2006 - 09:17 PM

Thank you! I don't know what I was doing wrong before but it's working now. Thanks so much again! ;)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users