Actionscript - 加载并播放另一个声音文件

时间:2013-01-19 22:11:12

标签: actionscript-3 actionscript

我正在播放声音文件,我希望onclick开始播放另一个文件。

您可以在以下示例中查看funcyion PlayAnother()

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);      
}       

但是我收到错误(函数调用错误,或者之前的调用失败)。

任何人都可以帮忙。

1 个答案:

答案 0 :(得分:1)

Flash正在抱怨,因为您正在将新文件加载到已有数据的Sound对象中。 (如果你查看Sound.load() here的文档,它会说“一旦在Sound对象上调用了load(),你就不能在以后将另一个声音文件加载到该Sound对象中” )。

您只需在加载File2之前实例化一个新声音并再次运行play()

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