Jump to content


Photo

funkysoul's MP3 Player in Flash with XML Tutorials


  • Please log in to reply
26 replies to this topic

#21 Ben

Ben

    P2L Jedi Master

  • Publishing Betazoids
  • PipPipPipPip
  • 1,366 posts
  • Gender:Male
  • Location:VIC, Australia

Posted 08 September 2006 - 01:25 AM

So, wait. You want the option of either loading an external text file by putting the file name in a xml attribute, or just loading the xml attribute text?

Well, here's a modified piece of the code you posted, hope it works for you:
stop();
playlist = new XML();
playlist.ignoreWhite = true;
playlist.onLoad = function(success) {
	if (success) {
		_global.songname = [];
		_global.songband = [];
		_global.songlyrics = [];
		_global.songext = [];
		_global.songfile = [];
		for (var i = 0; i<playlist.firstChild.childNodes.length; i++) {
			_global.songname[i] = playlist.firstChild.childNodes[i].attributes.name;
			_global.songband[i] = playlist.firstChild.childNodes[i].attributes.band;
			_global.songlyrics[i] = playlist.firstChild.childNodes[i].attributes.lyrics;
			_global.songext[i] = playlist.firstChild.childNodes[i].attributes.ext;
			_global.songfile[i] = playlist.firstChild.childNodes[i].attributes.file;
			// trace(songname[i]+"  "+songfile[i]+" "+songband[i]+"  "+songlyrics+");
		}
	}
	_root.createEmptyMovieClip("sound_mc", 1);
	_global.song_nr = random(0);
		_root.sound_mc.songStarter(songfile[song_nr], songname[song_nr], songband[song_nr], songlyrics[song_nr]);
};
function timer(sound_obj) {
	time = sound_obj.position/1000;
	min = Math.floor(time/60);
	min = (min<10) ? "0"+min : min;
	sec = Math.floor(time%60);
	sec = (sec<10) ? "0"+sec : sec;
	timeDisplay_txt.text = min+":"+sec;
}
MovieClip.prototype.songStarter = function(file, name, band, lyrics) {
	if (this.sound_obj) {
		this.sound_obj.stop();
		delete this.sound_obj;
	}
	this.sound_obj = new Sound(this);
	this.sound_obj.loadSound(file, true);
	this.onEnterFrame = function() {
		if (this.sound_obj.position>0) {
			delete this.onEnterFrame;
			this._parent.display_txt.text = name+" / "+band;
			if(songext[song_nr] == "true") {
				var loadLyrics:LoadVars = new LoadVars();
				loadLyrics.onLoad = function() {
					this._parent.lyrics_box.text=lyrics;
				}
				loadLyrics.load(songlyrics[song_nr]);
			} else {
				this._parent.lyrics_box.text=lyrics;
			}
			timeInterval = setInterval(timer, 1000, this.sound_obj);
		} else {
			this._parent.display_txt.text = "loading...";
		}
	};
	this.sound_obj.onSoundComplete = function() {
		clearInterval(timeInterval);
		this._parent.timeDisplay_txt.text = "00:00";
		(song_nr == songfile.length-1) ? _global.song_nr=0 : _global.song_nr++;
		_root.sound_mc.songStarter(songfile[song_nr], songname[song_nr], songband[song_nr], songlyrics[song_nr]);
	};
	this._parent.volume1.dragger.onPress = function() {
		startDrag(this, true, 0, this._y, this._parent.volBG._width, this._y);
		this.onEnterFrame = function() {
			var p = (this._x/this._parent.volBG._width)*100;
			this._parent._parent.sound_mc.sound_obj.setVolume(p);
		};
	};
	this._parent.volume1.dragger.onRelease = function() {
		delete this.onEnterFrame;
		stopDrag();
	};
	this._parent.volume1.dragger.onReleaseOutside = function() {
		stopDrag();
	};
};
btn_play.onRelease = function() {
	if (pause == true){ // no comment....
		this._parent.sound_mc.sound_obj.start(posiP) // start sound from the previously saved position
	}
	else {
	clearInterval(timeInterval);
	_root.timeDisplay_txt.text = "00:00";
	this._parent.sound_mc.songStarter(songfile[song_nr], songname[song_nr], songband[song_nr], songlyrics[song_nr]);
	}
};
btn_pause.onRelease = function() { //pause button function
	this._parent.sound_mc.sound_obj.stop(); //stop the current sound
	posiP = _root.sound_mc.sound_obj.position / 1000; // save the current position in a new variable and divide by 1000 (ms -> sec)
	pause = true;//set the variable pause to true
}
btn_fw.onRelease = function() {
	clearInterval(timeInterval);
	this._parent.timeDisplay_txt.text = "00:00";
	(song_nr == songfile.length-1) ? _global.song_nr=0 : _global.song_nr++;
	_root.sound_mc.songStarter(songfile[song_nr], songname[song_nr], songband[song_nr], songlyrics[song_nr]);
};
btn_rev.onRelease = function() {
	clearInterval(timeInterval);
	this._parent.timeDisplay_txt.text = "00:00";
	(song_nr == 0) ? _global.song_nr=songfile.length-1 : _global.song_nr--;
	_root.sound_mc.songStarter(songfile[song_nr], songname[song_nr], songband[song_nr], songlyrics[song_nr]);
};
playlist.load("playlist.xml");
Then, your text files with the lyrics in them, would look like this:
lyrics=lyricstext vlah balhalbroam
And your xml file's would look like what I wrote above.

#22 funkysoul

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 08 September 2006 - 03:53 AM

more elegant using one function and only one extra array :)

boogoloo1: Send me your email so I can mail you the zip :D


Just put this function at the end of your code:
function changeLyrics(a){
	lyricsText = a;
	loadVariablesNum (lyricsText, 0);
}

on every button put this line:
changeLyrics(lyrics[song_nr]);

don't forget to create the array, like we did before.

TXT file:
myText=this are my lyrics.. bla bla bla


XML:
<?xml version="1.0" encoding="UTF-8"?>
<songs>
<song name ="Test Track 1" band ="Band1" file="test1.mp3" lyric="test1.txt"/>
<song name ="Test Track 2" band ="Band2" file="test2.mp3" lyric="test2.txt"/>
<song name ="Test Track 3" band ="Band3" file="test3.mp3" lyric="test3.txt"/>
<song name ="Test Track 4" band ="Band4" file="test4.mp3" lyric="test4.txt"/>
</songs>

ACtionscript:
stop();
title_txt.autoSize = "left";
timeDisplay_txt.autoSize = "left";
toolTip._visible = false;
var amountLoaded:Number;
var duration:Number;
var lyricsText:String;

playlist = new XML();
playlist.ignoreWhite = true;
playlist.onLoad = function(success) {
	if (success) {
		_global.songname = [];
		_global.songband = [];
		_global.songfile = [];
		_global.lyrics = [];
		for (var i = 0; i<playlist.firstChild.childNodes.length; i++) {
		_global.songname[i] = playlist.firstChild.childNodes[i].attributes.name;
		_global.songband[i] = playlist.firstChild.childNodes[i].attributes.band;
		_global.songfile[i] = playlist.firstChild.childNodes[i].attributes.file;
		_global.lyrics[i] = playlist.firstChild.childNodes[i].attributes.lyric;
		attachMovie("butTemp","but"+i,i+50);
		eval("but"+i).id=i;
		_root["but"+i]._x = 5;
		_root["but"+i]._y = 40 + (i*15);
		_root["but"+i].but_txt.text = songname[i];
		if (i >= 3){
			_root["but"+i]._x = 160
			_root["but"+i]._y = -5 + (i*15);
		}
		_root["but"+i].onRelease = function(){
			clearInterval(timeInterval);
			_root.timeDisplay_txt.text = "00:00/00:00";
			_root.sound_mc.songStarter(songfile[this.id], lyrics[this.id]);
			changeLyrics(lyrics[this.id]);
		}
	}
	}
	_root.createEmptyMovieClip("sound_mc", 1);
	_global.song_nr = random(songfile.length);
	_root.sound_mc.songStarter(songfile[song_nr],lyrics[song_nr]);
	
};

function timer(sound_obj) {
	time = sound_obj.position/1000;
	min = Math.floor(time/60);
	min = (min<10) ? "0"+min : min;
	sec = Math.floor(time%60);
	sec = (sec<10) ? "0"+sec : sec;
	timeDisplay_txt.text = min+":"+sec+"/"+totalDuration;
}
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;
}
MovieClip.prototype.songStarter = function(file) {
	if (this.sound_obj) {
		this.sound_obj.stop();
		delete this.sound_obj;
	}
	this.sound_obj = new Sound(this);
	this.sound_obj.loadSound(file, true);
	this.sound_obj.setVolume(0);
	this.onEnterFrame = function() {
		if (this.sound_obj.position>0) {
			delete this.onEnterFrame;
			timeInterval = setInterval(timer, 1000, this.sound_obj);
			track = this.sound_obj.id3.songname;
			artist = this.sound_obj.id3.artist;
			this._parent.title_txt.text =artist+" - "+track;
		} else {
			this._parent.title_txt.text = "loading...";
		}

	};			
	this.sound_obj.onSoundComplete = function() {
		clearInterval(timeInterval);
		_root.timeDisplay_txt.text = "00:00/00:00";
		(song_nr == songfile.length-1) ? _global.song_nr=0 : _global.song_nr++;
		_root.sound_mc.songStarter(songfile[song_nr],lyrics[song_nr]);
		changeLyrics(lyrics[song_nr]);
	};
	this._parent.volume1.dragger.onPress = function() {
		startDrag(this, true, 0, this._y, this._parent.volBG._width, this._y);
		_root.toolTip._visible = true;
		setInterval(draggableTip,100);
		function draggableTip(){
			_root.toolTip._x = _root._xmouse;
		}
		this.onEnterFrame = function() {
			var p = (this._x/this._parent.volBG._width)*100;
			this._parent._parent.sound_mc.sound_obj.setVolume(p);
			
		};
	};
	this._parent.volume1.dragger.onRelease = function() {
		delete this.onEnterFrame;
		stopDrag();
		
	};
	this._parent.volume1.dragger.onReleaseOutside = function() {
		_root.toolTip._visible = false;
		stopDrag();
		
	};
};

function soundStatus(){
	var amountLoaded = _root.sound_mc.sound_obj.getBytesLoaded() / _root.sound_mc.sound_obj.getBytesTotal();
	_root.loader.loadBar._width = amountLoaded * 260;
	duration = _root.sound_mc.sound_obj.duration;
	position = _root.sound_mc.sound_obj.position;
	_root.playHead._x = position / duration * 272 + 5;
}

btn_play.onRelease = function() {
	clearInterval(timeInterval);
	_root.timeDisplay_txt.text = "00:00/00:00";
	this._parent.sound_mc.songStarter(songfile[song_nr],lyrics[song_nr]);
			changeLyrics(lyrics[song_nr]);
};
btn_stop.onRelease = function() {
	clearInterval(timeInterval);
	_root.timeDisplay_txt.text = "00:00/00:00";
	this._parent.sound_mc.sound_obj.stop();
};
btn_fw.onRelease = function() {
	clearInterval(timeInterval);
	_root.timeDisplay_txt.text = "00:00/00:00";
	(song_nr == songfile.length-1) ? _global.song_nr=0 : _global.song_nr++;
	_root.sound_mc.songStarter(songfile[song_nr],lyrics[song_nr]);
			changeLyrics(lyrics[song_nr]);

};
btn_rev.onRelease = function() {
	clearInterval(timeInterval);
	_root.timeDisplay_txt.text = "00:00/00:00";
	(song_nr == 0) ? _global.song_nr=songfile.length-1 : _global.song_nr--;
	_root.sound_mc.songStarter(songfile[song_nr],lyrics[song_nr]);
			changeLyrics(lyrics[song_nr]);
};
playlist.load("playlist.xml");
setInterval(duration,100);
setInterval(soundStatus,100);

function changeLyrics(a){
	lyricsText = a;
	loadVariablesNum (lyricsText, 0);
}


#23 boogooloo1

boogooloo1

    Young Padawan

  • Members
  • Pip
  • 116 posts

Posted 08 September 2006 - 07:12 AM

Thanks guys! Trying it in a minute!

I'll send you my email, funky :)

#24 boogooloo1

boogooloo1

    Young Padawan

  • Members
  • Pip
  • 116 posts

Posted 08 September 2006 - 07:37 AM

:wacko:

I've just realised your actionscript must be based on version 3 of your player, funky - I'm only using version 2! Anyway, I think some of the new features would be great, so I'll wait until I get the zip, and figure out how to apply it to my player :)

#25 funkysoul

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 08 September 2006 - 08:03 AM

yeh it' based on my last version..
Send me your fla file without all the extras.. and I will embed my code in your fla.. takes me 1min :) you have my email do you?

#26 boogooloo1

boogooloo1

    Young Padawan

  • Members
  • Pip
  • 116 posts

Posted 08 September 2006 - 05:05 PM

Don't think so! I gave you mine, though!

:D

#27 cupaball

cupaball

    Young Padawan

  • Members
  • Pip
  • 25 posts
  • Location:Philly

Posted 13 September 2006 - 03:32 PM

I was wondering if I could get the source files of the player...and I can try to go from there. I think my e-mail is in my profile.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users