0

我有这个功能,我把它放在一个应用程序中,可以在 Droid 和 IOS 设备上运行。

目的:它通过 10 个文件夹,每个文件夹都包含 mp3。它获取内容并从文件夹中随机选择,然后将该随机 mp3 与其他随机选择的 mp3 一起放入另一个数组中。这个最终数组是随机播放列表。

大多数情况下它都有效,但有时第一个 Var 留空。我想知道第二个数组是否正在尝试获取第一个数组的内容并且 cpu 运行缓慢,所以它有时可能不起作用。

有没有办法让这个代码遵循特定的顺序以确保正常运行?

function loadRandomMeditations(Language:String,CurrentMystery:String,CurrentDecade:String)
    {

    RandomSounds = [];
    TempArray = [];

    sounds = [];
    for(var a:int = 1; 11>a;a++)
        {
            var folder:String = "Languages/" + Language + "/Meditations/" + CurrentMystery + "/"+CurrentDecade+"/"+a+"/";
            var desktop:File = File.applicationDirectory.resolvePath(folder);
            var medSound:Array = desktop.getDirectoryListing();

            /// Looping thru to get all sounds in this directory
            for (var i:uint = 0; i < medSound.length; i++)
                {
                            // puts all sounds of folder into a temporary array
                TempArray.push(medSound[i].url);
                }

                    /// This counts the array and chooses a random number.
            var num = 1 + Math.round(Math.random()*(medSound.length-1));

                    //Adds Zero to make counting even
            TempArray.unshift("Zero");

            /// Puts the randomly selected array item into another array.
            RandomSounds.push(TempArray[num]);

            sounds[a] = new Sound(new URLRequest(TempArray[num]));
            TempArray = [];
        }
    trace(RandomSounds);

}

输出:有时第一个 mp3 没有加载到最终的播放列表数组中。

4

1 回答 1

0

你过于复杂了你的功能

function loadRandomMeditations(Language:String,CurrentMystery:String,CurrentDecade:String)
  {
  var medSound:Array = []
  var RandomSounds:Array = [];
  var sounds:Array = [];
  var folder:String = "Languages/" + Language + "/Meditations/" + CurrentMystery + "/"+CurrentDecade+"/";
  var desktop:File;
  var i:int;
  var num:int;
  // stay with the standard and use Lower Case I in a for loop
  for(i = 1; 11>i;i++)
    {
    desktop = File.applicationDirectory.resolvePath(folder+i+"/");
    medSound= desktop.getDirectoryListing();

    // This counts the array and chooses a random number.
    // not sure why you have a 1 + on this but it needs to go
    num = Math.round(Math.random()*(medSound.length-1));

    // why create a second "temp" array when you can just pick one off the medSound array
    // Puts the randomly selected array item into another array.
    RandomSounds.push(medSound[num].url);

    sounds[a] = new Sound(new URLRequest(medSound[num].url));
  }
  trace(RandomSounds);
}
于 2012-09-21T19:59:58.947 回答