0

DroidおよびIOSデバイスで実行するアプリに入れるこの機能があります。

目的: それぞれ mp3 を含む 10 個のフォルダーを通過します。コンテンツを取得し、フォルダーからランダムに選択し、そのランダムな mp3 を他のランダムに選択された mp3 と共に別の配列に配置します。この最後の配列は、ランダム プレイリストです。

ほとんどの場合は機能しますが、最初の Var が空白のままになることがあります。2 番目の配列が最初の配列の内容を取得しようとしていて、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 に答える