1

我想创建一个 .mp3 文件和字符串的数组列表。如果 arraylist 中的字符串由随机数调用,则将播放特定的 mp3 文件。我可以制作一个包含 .mp3 文件和字符串的数组列表,以便可以同时调用它们,或者我可以制作单独的数组列表。或者甚至不使用 .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

您想要的是一个将字符串与 MP3 文件路径相关联的Map 。

于 2013-02-16T05:14:44.480 回答
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 回答