[FMX] MP3 preloader and sound object

Hi all,
First time I post on this great website.

And… I have a problem… (no, really? hmmm… yes!)
Excuse my broken English (I will try to be clear).

I’m creating a MP3 jukebox for my website, a Movie Clip (MC) named “miniJuke”. A function “preloadSound” is called, and should preload my sound object. But it doesn’t work : I get a NaN until the movie is fully loaded and then I get 100.

Here is my code :

// the sound preloader function, where “pieceOfCake”
// is the path to find my MP3

function preloadSound(pieceOfCake){
this.createEmptyMovieClip (“tempLoader”,this);

tempLoader.onEnterFrame = function() {
var bl = mySound.getBytesLoaded();
var bt = mySound.getBytesTotal();
var p = isNaN(bt)?0 : Math.floor(bl*100/bt);

if (p < 100){
_root.miniJuke.percent.text = p;
trace (“that works ?”);
}else{
mySound.start (0,99);
mySound.isPlaying = true;
tempLoader.removeMovieClip();
}
};
mySound.loadSound(pieceOfCake,false);
};

“percent” is a textfield, “mySound” is the sound object.
The “trace” instruction (that works?) is never displayed, so, i think than “p” is never lower than “100”.

How to preload “mySound” correctly ?
I really need help.
Thx.

the only thing I can see is that you should check for the existance of bt aside from it just being NaN or not. Something like:

var p = (isNaN(bt) || !bt) ? 0 : Math.floor(bl*100/bt);

and that you should use
this.removeMovieClip();
to remove the empty clip (since the scope is within that clip)

I would also put
mySound.loadSound(pieceOfCake, false);
as the first thing, but that shouldnt really matter


// the sound preloader function, where "pieceOfCake"
// is the path to find my MP3

function preloadSound(pieceOfCake){
  this.createEmptyMovieClip ("tempLoader",this);

  tempLoader.onEnterFrame = function() {
    var bl = mySound.getBytesLoaded();
    var bt = mySound.getBytesTotal();
    var p = isNaN(bt)?0 : Math.floor(bl*100/bt);
    if (p < 100){
      _root.miniJuke.percent.text = p;
      trace ("that works ?");
    }else{
      mySound.start (0,99);
      mySound.isPlaying = true;
      tempLoader.removeMovieClip();
    }
  };

  mySound.loadSound(pieceOfCake,false);
};

Ok, I haven’t changed anything in your code, I just reposted it for clarity.

You should take Senocular’s suggestion and move the loadSound call to the first thing after defining the preload function.

Make sure that you have already created a sound object with: [COLOR=seagreen]mySound = new Sound(targetMC)[/COLOR], because it seems to me that that is the problem. Your bt and bl variables currently are checking the bytes total and loaded of the mySound object, but that object is just an instance of the generic Object object, so it is fully loaded once the whole movie is fully loaded.

-Al

Ok, thank you,

But I found what it was wrong. I try to explain you with my poor English :slight_smile: … my mp3 player was composed of three function :

-> the first, to create a track list from an external file, and display it on a comboBox, here named “stackList” :

maListe = new LoadVars();
maListe.load("jukebox/liste.txt");
maListe.onLoad = function (ok){
	var i=0;
	for(i=1; i <= maListe.nbreSons; i++){
		stackList.addItem(maListe["son"+i]);
	}
};

-> in a second part, a function to get the right path in the list, according to comboBox (change Handler “choisirSon”).

function choisirSon(){
	var morceau;
	morceau = "jukebox/" + stackList.getSelectedItem().label + "**.tko**";
	if (Number(stackList.getSelectedIndex()) != 0){
		preloadSound(morceau);
	}
};

-> and finally, the preloader :

mySound = new Sound(this);

function preloadSound(pieceOfCake){
	this.createEmptyMovieClip ("tempLoader",this);
	
	tempLoader.onEnterFrame = function() {
		var bt = mySound.getBytesTotal();
		var bl = mySound.getBytesLoaded();		
		var calcul = ! bt ? 0 : Math.floor(bl*100/bt);

		if (calcul == 100){
			_root.miniJuke.pourcentage = "";
			mySound.start(0,99);
			mySound.isPlaying=true;
			this.removeMovieClip();			
		}else{
			_root.miniJuke.pourcentage = calcul;
			_root.miniJuke.progression._xscale = calcul;
		}
	};
	mySound.loadSound(pieceOfCake,false);
};

The problem came from the fact than I changed my “.mp3" extensions to ".tko” extensions. For example, “track1.mp3” was “track1.tko”. On the HD, the player works. But nothing online.

So, I renamed the extensions correctly, with “*.mp3”, and I changed the second function :

function choisirSon(){
	var morceau;
	morceau = "jukebox/" + stackList.getSelectedItem().label + "**.MP3**";
	if (Number(stackList.getSelectedIndex()) != 0){
		preloadSound(morceau);
	}
};

And now, that works !!!
Tada !!!

Thx.
<:}