1

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

您可以在以下示例中检查函数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);      
}       

但我得到了错误(函数调用顺序不正确,或者之前的调用不成功)。

任何人都可以帮忙吗?

4

1 回答 1

1

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

你只需要在加载 File2 之前实例化一个新的 Sound 并play()再次运行:

private function PlayAnother(e:MouseEvent):void
{           
    mySoundChannel.stop();
    TheSound = new Sound();
    TheSound.load(new URLRequest("../lib/File2.MP3"));      
    mySoundChannel = TheSound.play();    
}
于 2013-01-20T01:44:26.707 回答