Greetings people. I have a problem with sound, after 20-30 sec of playing it stops. I’m using this sound singleton to play it, if anyone know a solution for this- please help. Functions “nokti” and “nykti” are used to fade the sound in and out. I never actually call any of methods anywhere, and the sound dies…
package main{
import flash.media.Sound;
import flash.net.URLRequest;
import flash.utils.*;
import flash.media.SoundTransform;
import flash.media.SoundChannel;
import flash.media.SoundLoaderContext;
import flash.events.Event;
public class soundSingleton {
private static var INSTANCE:soundSingleton = new main.soundSingleton();
var my_sound:Sound;
var soundURL:URLRequest;
var isPlaying:Boolean=false;
var intervalID:Number;
var volume:Number=1;
var context:SoundLoaderContext;
private var channel:SoundChannel;
public function soundSingleton() {
if (INSTANCE) {
throw new Error("Error: Instantiation failed: Use SingletonDemo.getInstance() instead of new.");
} else {
this.soundURL=new URLRequest("sound.mp3");
this.my_sound= new Sound();
this.context= new SoundLoaderContext(50,false);
this.my_sound.load(this.soundURL, this.context);
//this.my_sound.load(this.soundURL);
this.channel=this.my_sound.play();
this.isPlaying=true;
this.setVolume(1);
this.my_sound.addEventListener(Event.COMPLETE,this.soundCompleted);
}
}
public static function getInstance():soundSingleton {
return INSTANCE;
}
function toggleSound():void {
if (this.isPlaying) {
this.isPlaying=false;
clearInterval(this.intervalID);
this.intervalID=setInterval(this.nykti,10);
} else {
this.isPlaying=true;
clearInterval(this.intervalID);
this.intervalID=setInterval(this.nokti,10);
}
}
private function setVolume(volume:Number):void {
var transform:SoundTransform = this.channel.soundTransform;
transform.volume = volume;
this.channel.soundTransform = transform;
}
function nykti():void {
var transform:SoundTransform = this.channel.soundTransform;
var Volume:Number=transform.volume;
if (Volume>=0) {
Volume=Volume-0.05;
setVolume(Volume);
} else {
setVolume(0);
clearInterval(this.intervalID);
}
}
function nokti():void {
var transform:SoundTransform = this.channel.soundTransform;
var Volume:Number=transform.volume;
if (Volume==0) {
this.channel=this.my_sound.play();
}
if (Volume<1) {
Volume=Volume+0.05;
setVolume(Volume);
} else {
setVolume(1);
clearInterval(this.intervalID);
}
}
function soundCompleted(event:Event):void{
if(event){
//this.my_sound.play();
}
}
}
}