我想创建一个.mp3文件和字符串的arraylist。如果arraylist中的字符串被随机数调用,则播放特定的mp3文件。我可以制作一个充满.mp3文件和字符串的arraylist,以便可以同时调用它们,或者我可以创建单独的数组列表。或者甚至不使用.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
用最少量的硬编码实现这一目标的最佳方法是什么?
答案 0 :(得分:0)
你想要的是一个Map,它将字符串与MP3文件路径相关联。
答案 1 :(得分:0)
您可以按如下方式创建自己的自定义对象arraylist ..
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>();
答案 2 :(得分: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();
}
}
}