1

.mp3 ファイルと文字列の配列リストを作成したいと考えています。arraylist 内の文字列が乱数によって呼び出されると、特定の mp3 ファイルが再生されます。.mp3 ファイルと文字列の両方で満たされた arraylist を作成して、それらを同時に呼び出すことができるようにするか、個別の arrayLists を作成しますか? それとも、.mp3 ファイルの ArrayList を使用しないのでしょうか? ありがとうございました。

ArrayList<String? words = new ArrayList<String>
words.add("Hello World");

//On Button Click
//Generates randomNumber Integer
//randomNumber=1
//SetText to "Hello World" and play .mp3 that says "Hello World" simultaneously and put thread to sleep for .mp3 length

最小限のハードコーディングでこれを達成する最善の方法は?

4

3 に答える 3

0

独自のソングオブジェクトを作成し、そこからランダムに曲をピックアップします

public class Player {

    public static void main(String[] args) {
        Player player = new Player();
        //populate music in your arrayList
        List<Song> album = player.populateMusicList();
        //play
        for (int i = 0; i < 10; i++) {
            player.play(album);
        }
    }

    public void play(List<Song> album) {
        System.out.println("playing --" + album.get(this.fetchMusicRandomly(album)));
    }

    private int fetchMusicRandomly(List<Song> album) {
        return ThreadLocalRandom.current().nextInt(0, album.size());
    }

    private List<Song> populateMusicList() {
        List<Song> musicBucket = new ArrayList<Song>();
        musicBucket.add(new Song("musicName-1", "pathtomp3File"));
        musicBucket.add(new Song("musicName-2", "pathtomp3File"));
        musicBucket.add(new Song("musicName-3", "pathtomp3File"));
        musicBucket.add(new Song("musicName-4", "pathtomp3File"));
        musicBucket.add(new Song("musicName-5", "pathtomp3File"));
        musicBucket.add(new Song("musicName-6", "pathtomp3File"));
        musicBucket.add(new Song("musicName-7", "pathtomp3File"));
        musicBucket.add(new Song("musicName-8", "pathtomp3File"));
        musicBucket.add(new Song("musicName-9", "pathtomp3File"));
        musicBucket.add(new Song("musicName-10", "pathtomp3File"));
        return musicBucket;
    }

    class Song {

        public Song(String name, String pathToMp3) {
            this.name = name;
            this.pathToMp3 = pathToMp3;
        }
        String name;
        String pathToMp3;

        public String getName() {
            return name;
        }

        public String getPathToMp3() {
            return pathToMp3;
        }

        @Override
        public String toString() {
            StringBuilder result = new StringBuilder();
            result.append(" {Name: " + name + " }");
            result.append(" {Path To Mp3file: " + pathToMp3);
            result.append("}");
            return result.toString();
        }
    }
}
于 2013-02-16T15:49:34.037 に答える
0

次のように、オブジェクトの独自のカスタム配列リストを作成できます。

public class SongInfo
{
    String songName;
    String songPath;


    public SongInfo(String songName,String songPath){
        this.songName = songName;
        this.songPath = songPath;

    }
}

ArrayList<SongInfo> customSongList = new ArrayList<SongInfo>();
于 2013-02-16T08:59:21.167 に答える
0

必要なのは、文字列を MP3 ファイル パスに関連付けるマップです。

于 2013-02-16T05:14:44.480 に答える