# Problem with sound.duration

**URL:** <https://forum.kirupa.com/t/problem-with-sound-duration/34438>\
**Category:** Uncategorized\
**Created:** [November 2, 2003, 1:03pm UTC](https://forum.kirupa.com/t/problem-with-sound-duration/34438 "2003-11-02T13:03:58Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Flashmatazz](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/flashmatazz/32/140_2.png) [@Flashmatazz](https://forum.kirupa.com/u/Flashmatazz)\
**Post date:** [November 2, 2003, 1:03pm UTC](https://forum.kirupa.com/t/problem-with-sound-duration/34438/1 "2003-11-02T13:03:58Z")

</div>

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:

```auto

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:

```auto

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:

```auto

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 4, 2003, 4:25pm UTC](https://forum.kirupa.com/t/problem-with-sound-duration/34438/2 "2003-11-04T16:25:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 4, 2003, 4:32pm UTC](https://forum.kirupa.com/t/problem-with-sound-duration/34438/3 "2003-11-04T16:32:58Z")

</div>

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

[Here](http://www.twc.sshunet.nl/~swmvanbo/MT/flash/audioplayer.fla) it is.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 5, 2003, 6:08am UTC](https://forum.kirupa.com/t/problem-with-sound-duration/34438/4 "2003-11-05T06:08:21Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 5, 2003, 6:52am UTC](https://forum.kirupa.com/t/problem-with-sound-duration/34438/5 "2003-11-05T06:52:21Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 5, 2003, 11:15pm UTC](https://forum.kirupa.com/t/problem-with-sound-duration/34438/6 "2003-11-05T23:15:01Z")

</div>

Thanks a lot. 🙂  
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

```auto

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

```

and in the onEnterFrame action

```auto

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:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 6, 2003, 6:13pm UTC](https://forum.kirupa.com/t/problem-with-sound-duration/34438/7 "2003-11-06T18:13:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 6, 2003, 11:09pm UTC](https://forum.kirupa.com/t/problem-with-sound-duration/34438/8 "2003-11-06T23:09:49Z")

</div>

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

[audioplayer](http://www.twc.sshunet.nl/~swmvanbo/MT/flash/audioplayer2.fla)

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:

```auto

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 7, 2003, 7:50pm UTC](https://forum.kirupa.com/t/problem-with-sound-duration/34438/9 "2003-11-07T19:50:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 9, 2003, 7:03pm UTC](https://forum.kirupa.com/t/problem-with-sound-duration/34438/10 "2003-11-09T19:03:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 10, 2003, 9:01pm UTC](https://forum.kirupa.com/t/problem-with-sound-duration/34438/11 "2003-11-10T21:01:52Z")

</div>

Voetsjoeba, any luck yet?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 10, 2003, 9:20pm UTC](https://forum.kirupa.com/t/problem-with-sound-duration/34438/12 "2003-11-10T21:20:44Z")

</div>

Woops, sorry, I kinda forgot about this thread 😊

I’ll have a look at it again 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 11, 2003, 9:11am UTC](https://forum.kirupa.com/t/problem-with-sound-duration/34438/13 "2003-11-11T09:11:38Z")

</div>

Many thanks 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2003, 12:12pm UTC](https://forum.kirupa.com/t/problem-with-sound-duration/34438/14 "2003-11-12T12:12:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2003, 2:28pm UTC](https://forum.kirupa.com/t/problem-with-sound-duration/34438/15 "2003-11-12T14:28:57Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2003, 3:21pm UTC](https://forum.kirupa.com/t/problem-with-sound-duration/34438/16 "2003-11-12T15:21:42Z")

</div>

> \*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 (-:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2003, 4:04pm UTC](https://forum.kirupa.com/t/problem-with-sound-duration/34438/17 "2003-11-12T16:04:02Z")

</div>

Geeeezzzz, why haven’t I tried this before 😊

[http://www.twc.sshunet.nl/~swmvanbo/MT/flash/audioplayer3.fla](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 🙂
