我很少在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.
我也尝试过completeListener:
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或逐个外部加载它们。
再次感谢!
答案 0 :(得分:0)
main.as文件的代码
http://www.kirupa.com/forum/showthread.php?305098-Playing-a-embedded-sound-in-an-external-swf
package
{
import flash.display.*;
import flash.media.*;
import flash.events.*;
import flash.net.*;
public class Main extends MovieClip
{
var swf:MovieClip;
public function Main()
{
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded);
l.load( new URLRequest("child.swf") );
}
private function swfLoaded(e:Event):void
{
swf = e.target.content as MovieClip;
addChild(swf);
swf.playSound();
}
}
}
加载swf的代码“child.as”
package
{
import flash.display.*;
import flash.media.*;
import flash.events.*;
import flash.net.*;
public class Child extends MovieClip
{
private var s:Sound1;
private var sc:SoundChannel;
public function Child()
{
s = new Sound1();
sc = new SoundChannel();
// All of the following lines are actually optional, if all your graphic elements are already inside the swf prepared in flash pro
var g:Graphics = this.graphics;
g.beginFill(0x666666);
g.drawRect(0,0,550,400);
g.endFill();
}
public function playSound():void
{
sc = s.play();
}
}
}
Adobe论坛上的用户“kglad.com”也给了我一个很好的答案: