i am new to action script 3 and i have one problem.am loading the audio from the XML files.
While clicking on button am loading the audio.i have 4 buttons and 4 audios are there in xml.When i click on one it is loading the audio, but when i click 2 , 3 and 4 the audio is not loading
it is throwing error
Error: Error #2037: Functions called in incorrect sequence, or earlier call was unsuccessful.
at flash.media::Sound/_load()
at flash.media::Sound/load()
at AnswerQuestions_as3_fla::MainTimeline/showans()
This is the code
sound.load(new URLRequest(audio_arr[aud_num-1]+".mp3"));
sound.addEventListener(Event.COMPLETE,loadaud);
schannel.addEventListener(Event.SOUND_COMPLETE,stopaud);
function loadaud(e:Event) {
trace("play aud");
schannel=sound.play();
}
function stopaud(e:Event) {
trace("audio stop");
schannel.stop();
sound.close();
}
Have you tested audio_arr[aud_num-1]+".mp3" to see what it returns? Are you sure that you're getting the file name that you expect?
From what you have it looks like your array audio_arr contains the names of the sound files. It might be simpler to add the ".mp3" extension to each file name in the array rather than concatenating at the URLRequest(). If I were doing this, I would get the file name in one step assign it to a local variable and then use that variable in the URLRequest().
Then I'm guessing that the problem is with the way that you are defining the sound object and the sound channel object. It might work best if you define four unique sound objects, then assign one sound file to each. You can use the same sound channel to play each of the sounds if you like. But you may find that it is easier to keep track of things if you assign a unique sound channel to each sound.
Hi,
If you're using multiple sounds you can use the following class, I have used it and it is working fine.
package classes.manager
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
import flash.media.SoundTransform;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.utils.Dictionary;
import classes.bean.staticClass;
import classes.application.Application;
import classes.manager.AppManager;
public class SoundManager extends MovieClip
{
private var _loadSound:Sound;
private var channel:SoundChannel;
private var _loadSoundArr:Array = new Array();
private var _soundTransform:SoundTransform;
private var SOUND_LOAD_IN_PROGRESS:Boolean;
private var soundDict:Dictionary;
public static var instance:SoundManager;
public var counter:int = 0;
private var arrayCount:int = 0;
/**Construtor*/
public function SoundManager()
{;
instance = this;
channel = new SoundChannel();
soundDict = new Dictionary(true);
channel.addEventListener(Event.SOUND_COMPLETE, soundCompleteHandler);
}
public static function getInstance():SoundManager
{
return instance as SoundManager;
}
/**Set the sound pass as string to push in array.*/
public function setLoadSounds(soundIDParam:String, soundPathParam:String )
{
_loadSoundArr.push( { soundID:soundIDParam, soundPath:soundPathParam } );
soundLoader();
}
/**Load Sound*/
public function soundLoader()
{
if(_loadSoundArr.length > 0 && SOUND_LOAD_IN_PROGRESS == false)
{
SOUND_LOAD_IN_PROGRESS = true;
_loadSound = new Sound();
_loadSound.load(new URLRequest(_loadSoundArr[0].soundPath));
_loadSound.addEventListener(Event.COMPLETE, soundLoadCompleteHandler);
_loadSound.addEventListener(IOErrorEvent.IO_ERROR , showError);
_loadSound.addEventListener(IOErrorEvent.NETWORK_ ERROR, showError);
_loadSound.addEventListener(SecurityErrorEvent.SE CURITY_ERROR, showSecurityError);
}
}
private function showError(e:IOErrorEvent)
{
//trace("SoundManager.showError");
trace(e);
}
private function showSecurityError(e:SecurityErrorEvent)
{
//trace("SoundManager.showSecurityError");
trace(e);
}
/**Sound complete handler,push sound into array*/
private function soundLoadCompleteHandler(evt:Event):void
{
staticClass.loadedSoundArr.push( { soundID:_loadSoundArr[0].soundID, soundPath:_loadSoundArr[0].soundPath, soundObj:evt.target} )
// taken [0] due to every time we shifting array, so we will load first value only
SOUND_LOAD_IN_PROGRESS = false;
var soundObj = new Object();
var sound:Sound = (evt.target) as Sound;
soundObj.snd = sound;
soundObj.channel = new SoundChannel();
soundObj.name = _loadSoundArr[0].soundID;
soundDict[_loadSoundArr[0].soundID] = soundObj;
_loadSoundArr.shift();
soundLoader();
}
/**Play sound*/
public function playSound(soundIDParam:String)
{
var sound:Object = soundDict[soundIDParam];
try
{
//Application.getInstance().debug.appendText("|tr y block|");
sound.channel = sound.snd.play();
}
catch (err:Error)
{
Application.getInstance().debug.appendText("|catc h| " + err.message + "|");
}
}
/**Stop sound*/
public function stopSound(soundIDParam:String)
{
var sound:Object = soundDict[soundIDParam];
sound.channel.stop();
}
/** Do mute or unmute sound*/
/*public function soundStateChange():void
{
//trace("SoundManager.soundStateChange");
if(staticClass.SOUND_MUTE == true)
{
muteSound();
}else
{
unMuteSound();
}
}*/
/** Do mute sound*/
/*private function muteSound():void
{
//trace("SoundManager.muteSound");
SoundMixer.soundTransform = new SoundTransform(0);
staticClass.SOUND_MUTE = false;
}*/
/** Do unmute sound*/
/*private function unMuteSound():void
{
//trace("SoundManager.unMuteSound");
SoundMixer.soundTransform = new SoundTransform(1);
staticClass.SOUND_MUTE = true;
}*/
/*public function getSoundChannel():SoundChannel
{
return channel;
}*/
private function soundCompleteHandler(e:Event)
{}
}//Class Ends
}// Package Ends
I have customized it according to my need, edit it according to your need.
Regards,
Vipul
North America
Europe, Middle East and Africa
Asia Pacific