我目前在Flashdevelop上从事一个项目,我想添加一些音乐。我正在开发游戏,因此不能选择多个文件。当前,所有资源都是嵌入式的,简单的预加载器可以加载所有内容(例如flashdevelop默认的预加载器)。
我不想在一开始就加载音乐,我想在需要时流式传输音乐。
可以播放嵌入的声音吗?
如果没有,是否可以将这些文件嵌入.swf文件中并稍后加载?
提前致谢!
最佳答案
您可以做两件事。一种是在初始加载完成后开始加载声音,然后将它们保存在Dictionary
中。其次是从Flash导出RSL(运行时共享库),它是一个SWF文件,您可以加载该文件并可以访问其中定义的所有类。
在第一种方法中,您基本上会像这样加载所有声音并将其保存到字典中:
import flash.media.Sound;
import flash.events.Event;
import flash.net.URLRequest;
import flash.utils.Dictionary;
var mSounds:Dictionary = new Dictionary();
function loadSound(url:String, soundName:String)
{
var sound:Sound = new Sound();
sound.addEventListener(Event.COMPLETE, onSoundLoadComplete);
sound.load(new URLRequest(url));
function onSoundLoadComplete(e:Event):void
{
sound.removeEventListener(Event.COMPLETE, onSoundLoadComplete);
trace(soundName,"Sound Loaded");
mSounds[soundName] = sound; // save it to dictionary
// then you can load it from dictionary
// using the name you assigned
if(mSounds["crystalised"])
(mSounds["crystalised"] as Sound).play();
}
}
loadSound("C:\\Users\\Gio\\Desktop\\Crystalised.mp3", "crystalised");
在第二种方法中,您必须执行更多步骤,但是只需加载一次。我将在这里列出步骤:
import flash.display.Loader;
import flash.system.LoaderContext;
import flash.system.ApplicationDomain;
import flash.system.SecurityDomain;
import flash.events.Event;
import flash.net.URLRequest;
import flash.media.Sound;
import flash.utils.getDefinitionByName;
function loadRSL(url:String):void
{
var loader:Loader = new Loader();
var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onRSLLoadComplete);
loader.load(new URLRequest(url), context);
function onRSLLoadComplete(e:Event):void
{
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onRSLLoadComplete);
trace("RSL Loaded");
// creating a new instance of the sound which is defined in RSL
var soundClass:Class = getDefinitionByName("Crystalised") as Class;
var sound:Sound = (new soundClass() as Sound);
sound.play();
}
}
loadRSL("SoundLibrary.swf");
关于actionscript-3 - Flash/AS3-预加载或流式嵌入声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17644539/