Hi, I have some trouble with pausing and then resuming sounds in as3.
package {
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.*;
public class Sounds {
var _main:Object;
var soundsOn:Boolean;
var songStarted:Boolean;
var lastPosition:Number = 0;
var fail_sound:Sound = new Shock();
var audience:Sound = new audienceSound();
var song_1:Sound = new song1();
var song_2:Sound = new song2();
//var background_music:SoundChannel;
var channel:SoundChannel // = new SoundChannel();
//var song_3:Sound = new song3();
public function Sounds(m:Object):void {
_main = m;
soundsOn = true;
channel = new SoundChannel();
}
public function init_sounds():void {
songStarted = false;
channel = audience.play();
channel.addEventListener(Event.SOUND_COMPLETE, startSong);
}
private function startSong(e:Event):void {
songStarted = true;
channel.removeEventListener(Event.SOUND_COMPLETE, startSong);
channel = song_1.play();
channel.addEventListener(Event.SOUND_COMPLETE, end);
}
public function pauseSong():void {
channel.stop();
lastPosition = channel.position;
trace(lastPosition);
channel.removeEventListener(Event.SOUND_COMPLETE, startSong);
}
public function continueSong():void {
//background_music.play(lastPosition);
trace(lastPosition);
if (songStarted) {
channel = song_1.play(lastPosition);
channel.addEventListener(Event.SOUND_COMPLETE, end);
}
else {
channel = audience.play(lastPosition);
channel.addEventListener(Event.SOUND_COMPLETE, startSong);
}
}
Pausing goes fine, however when I resume the song again, the sound starts playing a few seconds before I actually paused (so parts of the sound repeats itself). This is really annoying. Is there any way to fix this problem?
Thanks!