2013-01-19 41 views
1

我正在播放一个声音文件,我想onclick开始播放另一个文件。Actionscript - 加载并播放另一个声音文件

您可以检查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

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

你只需要加载文件2之前实例化一个新的声音,然后再次运行play()

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

就是这样, 非常感谢 – user1386213