Problem with sound.duration

I’ll probably feel embarrassed for overseeing something, but at the moment I’m getting a little frustrated with this, so here goes:

I’m building a simple audioplayer.

In frame 1 I have:


mySound = new Sound();
songs = new Array("1.mp3", "2.mp3");
songIndex = 0;

mySound.loadSound(this.songs[songIndex], true);

mySound.start();
var playing = true;

this.onEnterFrame = function() {
	if (playing == true){
		totalDuration = mySound.duration/1000;
		totalSeconds = Math.floor(totalDuration%60);
		totalMinutes = Math.floor((totalDuration - totalSeconds)/60);
		if(totalSeconds < 10) totalSeconds = "0" + totalSeconds;
		this.display.track = songs[songIndex] + " - " + totalMinutes + ":" + totalSeconds;
	
		currentPosition = mySound.position/1000;
		currentSeconds = Math.floor(currentPosition%60);
		currentMinutes = Math.floor((currentPosition - currentSeconds)/60);
		if(currentSeconds < 10) currentSeconds = "0" + currentSeconds;
		this.display.timer = currentMinutes + ":" + currentSeconds;
	}
	else {
		currentSeconds = 0;
		currentMinutes = 0;
		this.display.timer = "0:00";
	}
}
MovieClip.prototype.changeTrack = function(x){
	this.songIndex = (this.songIndex+x)%this.songs.length;
	if (this.songIndex<0) {
		this.songIndex += this.songs.length;
	}
	mySound.stop();
	mySound.loadSound(this.songs[songIndex], true);
	mySound.start();
}

On my stop-button I have:


on(release){
	if (_root.playing == true){
		_root.mySound.stop();
		_root.playing = false;
	}
}

Ok, so the sound stops, no problem thusfar.

On my play-button I have:


on(release){
	if(_root.playing != true) {
		_root.mySound.loadSound(_root.songs[songIndex], true);
		_root.mySound.start();
		_root.playing = true;
	}
}

So, the sound begins to play from the beginning. However the problem is with the timer: when ‘stop’ is pressed it says 0:00

But, when I press play again it immediately jumps to the time it showed when stop was pushed, e.g. 0:12

Please shed a light on this for me, 'cause I’m very confused. :h:

Thanks.

Can we see your fla please ? Just to have the structure of your file, so we don’t have to reconstruct everything.

Yes, that would be handy wouldn’t it?
Dunno why I hadn’t uploaded it in the first place :sigh:

Here it is.

one thing that is not needed:

mySound.start();

when you use:

mySound.loadSound(this.songs[songIndex], true);

the sound will automatically start playing after a certain time period (namely the global _soundbuftime).

but this probably won’t fix anything…

This one was quite a tough one, but I managed to figure it out after I saw this sentence at the ActionScript dictionary: “If the sound is looped, the position will be reset to 0 at the beginning of each loop”. So I figured it didn’t for streaming non looping sounds, and indeed. Here you are :slight_smile:

Thanks a lot. :slight_smile:
I tried it and it worked when the sound was stopped when playing less than 1 minute. However, I understand the principle (I think…) and made some small changes to the code.

instead of 1 variable ‘alreadyplayed’ I now have 2 variables: ‘secondsPlayed’ and ‘minutesPlayed’.

On the stop-button, I now have


_root.secondsPlayed = Math.floor((_root.mySound.position/1000)%60);
_root.minutesPlayed = Math.floor((_root.mySound.position/1000)/60);

and in the onEnterFrame action


currentSeconds = Math.abs(_root.secondsPlayed-Math.floor((mySound.position/1000)%60));
currentMinutes = Math.abs(_root.minutesPlayed-Math.floor(((mySound.position/1000)-currentSeconds)/60));

Everything seems to work ok now.

However I’m still very confused about this. Why wouldn’t the sound.position be reset to 0 when there it’s stopped and the play -butoon has a loadSound action on it.
It makes no sense to me at all, I’m afraid… :q:

Oh right, sorry, I forgot about the minutes. I think the reason why it isn’t set to zero is because it’s streaming.

:bounce: :bounce: …still not working properly.

audioplayer

Let’s say I have 1.mp3 with duration = 0:20
If I stop it after 5 secs and then press play again, the sound starts over and the timer starts at 0:00 again but now stops at 0:15, so 5 secs before the sound is actually finished.

There also is a problem when the changeTrack function is called when the sound finishes or when you press the ‘previous’ or ‘next’ button:


trace(mySound.position/1000);

which is called onEnterFrame just shows the length of the next sound and not the actual position.

Can anybody please help me out on this 'cause it’s really annoying me.
Isn’t there a way to somehow reset the sound.position manually??

I hope this can be solved.
Many thanks.

position is a read-only property, so I’m afraid you can’t set it manually. I think I’m getting there, hang on.

I hope you can solve it. Thanks for the effort :thumb:

Voetsjoeba, any luck yet?

Woops, sorry, I kinda forgot about this thread :blush:

I’ll have a look at it again :slight_smile:

Many thanks :slight_smile:

Please people, I really want to know how to solve this :bounce:

This is getting on my nerves, it never works the way it’s supposed to. I suggest we go another way: use setInterval to update the timer. Way easier to control, and you can practically do anything with it, plus, easily resetting it. You know how to use setInterval or do you need an example ?

*Originally posted by Voetsjoeba *
This is getting on my nerves

same here.

anyway, I’ll give it a shot using setInterval. And if I don’t succeed…I’ll be back (-:

Geeeezzzz, why haven’t I tried this before :blush:

http://www.twc.sshunet.nl/~swmvanbo/MT/flash/audioplayer3.fla

everything seems to work now (code may have become a little unstructured though)

Thanks for the suggestions Voetsjoeba :slight_smile: