我很少在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中,也不能将它们逐个外部单独加载。
再次感谢!
最佳答案
来自Kirupa的用户SnickelFritz用简单得多的代码给了我一个神话般的答案,当使用多种声音时,这可能非常强大。这非常好,因为每个SWF只能使用一个预加载器,而不是每个文件使用多个加载器:
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();
}
}
}
加载的瑞士法郎“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”也给了我一个很好的答案:
http://forums.adobe.com/message/5681137#5681137
关于actionscript-3 - AS3:如何访问和控制外部SWF中的嵌入式声音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18798233/