user1386213
user1386213

Reputation: 1021

Actionscript - load and play another sound file

i am playing a sound file and i want onclick to start playing another file.

You can check the funcyion PlayAnother() in the following example:

private var TheSound:Sound = new Sound();           
private var mySoundChannel:SoundChannel = new SoundChannel();

private function PlaySound(e:MouseEvent):void
{       
    TheSound.load(new URLRequest("../lib/File1.MP3"));
    mySoundChannel = TheSound.play();
}

private function PlayAnother(e:MouseEvent):void
{           
    mySoundChannel.stop();
    TheSound.load(new URLRequest("../lib/File2.MP3"));          
}

public function Test1():void 
{
    var Viewer:Shape = new Shape();
    Viewer.graphics.lineStyle(0, 0x000000);
    Viewer.graphics.beginFill(0x000000);
    Viewer.graphics.drawRect(0, 0, 1, 10);
    Viewer.graphics.endFill();  
    Viewer.width = 30;
    Viewer.x = 10;

    var Viewer1:Shape = new Shape();
    Viewer1.graphics.lineStyle(0, 0x000000);
    Viewer1.graphics.beginFill(0x000000);
    Viewer1.graphics.drawRect(0, 0, 1, 10);
    Viewer1.graphics.endFill();         
    Viewer1.width = 30;
    Viewer1.x = 50;

    var tileSpot:Sprite = new Sprite();
    var tileSpot1:Sprite = new Sprite();
    tileSpot.addChild(Viewer)
    tileSpot1.addChild(Viewer1)
    addChild(tileSpot);
    addChild(tileSpot1);

    tileSpot.addEventListener(MouseEvent.CLICK, PlaySound);
    tileSpot1.addEventListener(MouseEvent.CLICK, PlayAnother);      
}       

but i'm getting the error (Functions called in incorrect sequence, or earlier call was unsuccessful).

can any one help please.

Upvotes: 1

Views: 208

Answers (1)

David Mear
David Mear

Reputation: 2254

Flash is complaining because you're loading a new file into a Sound object which already has data. (If you look at the docs for Sound.load() here, it says "Once load() is called on a Sound object, you can't later load a different sound file into that Sound object").

You just need to instantiate a new Sound before loading File2 and run play() again:

private function PlayAnother(e:MouseEvent):void
{           
    mySoundChannel.stop();
    TheSound = new Sound();
    TheSound.load(new URLRequest("../lib/File2.MP3"));      
    mySoundChannel = TheSound.play();    
}

Upvotes: 1

Related Questions