我很少在 AS3/Flash 中使用声音。我正在使用 Flash Pro CS6,但我似乎无法弄清楚如何访问、控制(播放、停止等)嵌入在加载到主 SWF 中的外部 SWF 中的声音。
当嵌入到主 swf 中时,很容易控制它们。但是,在外部加载的 SWR 上,我会遇到各种错误。对于这个应用程序,我真的需要将它们嵌入到外部 SWF 中。
我阅读了几种解决方案,但似乎都没有。
我使用 Flash CS6 导入工具将声音嵌入了一个名为 soundSegment1.mp3 的 mp3 文件,然后在 Flash 上打开 actionscript 属性面板以选择类名:SoundSegment1。然后我编辑类代码并创建一个名为 SoundSegment1.as 的文件,它保存在我的文档类 main.as 旁边的同一目录中。SoundSegment1 类的代码如下所示:
package {
import flash.media.*;
public class SoundSegment1 extends Sound
{
public function SoundSegment1 ()
{
// no code in here
}
public function playSound()
{
var soundSegment1:Sound = new SoundSegment1();
var channel:SoundChannel = soundSegment1.play();
}
}
}
然后,在我的 main.as 中,我做了几次尝试来播放这种声音,例如:
var fileLocation:URLRequest = new URLRequest(SWFToLoad);
loader.load(fileLocation);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressListener);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeListener);
loader.contentLoaderInfo.addEventListener(Event.INIT, initListener);
function initListener(e:Event):void // I also placed this code on the completeListener and it didn't work
{
loader.content.soundSegment1.playSound(); // doesn't work.
}
我得到:
Line XXX 1061: Call to a possibly undefined method playSound through a reference with static type flash.display:DisplayObject.
或者,我还读到我应该能够在 Main.as 文件中的任何地方做这样的事情:
var theClass:Class = Class(loader.content.getDefinitionByName("SoundSegment1"));
var theSound:theClass = new theClass();
theSound.play() //doesn't work either.
我还尝试了完整的侦听器:
var TheClass:Class = e.target.applicationDomain.getDefinition("SoundSegment1") as Class;
var theSound:TheClass = new TheClass();
theSound.play() //doesn't work either.
我得到:
ReferenceError: Error #1065: Variable SoundSegment1 is not defined.
at flash.system::ApplicationDomain/getDefinition()
我被困住了,我真的需要让它发挥作用。我会真诚地感谢您的帮助。
预先感谢您提供的任何帮助。我真的需要让它工作,因为我不能简单地将它们嵌入到主 SWF 中或从外部一个接一个地单独加载它们。
再次感谢!