as3 air检查文件是否已加载

时间:2013-05-18 06:20:50

标签: actionscript-3 air directory

如何运行代码然后阵列就绪?

当我运行以下代码时,我收到一条错误消息: TypeError:错误#1010:术语未定义且没有属性。

import flash.filesystem.File;

var desktop:File = File.applicationDirectory.resolvePath("sounds/drums");

var sounds:Array = desktop.getDirectoryListing();

for (var i:uint = 0; i < sounds.length; i++)
   {
    trace(sounds[i].nativePath); // gets the path of the files
    trace(sounds[i].name);// gets the name
   }


var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
mySound.load(new URLRequest("sounds/drums/"+sounds[i].name+""));
myChannel = mySound.play();

1 个答案:

答案 0 :(得分:2)

我通常使用类似下面的内容,每次加载声音时都会存储它并且计数器会递增,一旦加载所有声音,您就可以发送事件或开始播放loadedSounds中存储的任何声音

var sounds:Array = desktop.getDirectoryListing();

var loadedSounds:Object = {};
var soundsLoaded:int = 0;

for (var i:uint = 0; i < sounds.length; i++)
{
    var mySound:Sound = new Sound();
    mySound.addEventListener(Event.COMPLETE, onSoundLoaded);
    mySound.load(new URLRequest("sounds/drums/"+sounds[i].name));
}

private function onSoundLoaded(e:Event):void
{
    var loadedSound = e.target as Sound;
    // just get the file name without path and use it as key
    var lastIndex:int = loadedSound.url.lastIndexOf("/");
    var key:String = loadedSound.url.substr(lastIndex+1);

    // store sounds for later reference
    loadedSounds[key] = loadedSound ;

    soundsLoaded++;
    if (soundsLoaded == sounds.length)
    {
        //all sounds loaded, can start playing
    }
}
相关问题