2013-02-16 3 views
1

Je souhaite créer une liste de fichiers et de chaînes .mp3. Si une chaîne de l'arraylist est appelée par un numéro aléatoire, un fichier mp3 spécifique sera lu. Est-ce que je peux faire un arraylist rempli des fichiers .mp3 et des chaînes pour qu'ils puissent être appelés simultanément ou devrais-je faire des arrayLists séparés. Ou peut-être même pas utiliser un ArrayList de fichiers .mp3? Je vous remercie.Android, Création d'une liste de fichiers .mp3?

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 

Quelle est la meilleure façon d'y parvenir avec un minimum de codage en dur?

Répondre

0

Ce que vous voulez, c'est un Map qui corrèle les chaînes aux chemins de fichiers MP3.

0

Vous pouvez créer votre propre arraylist personnalisé d'objets comme suit ..

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>(); 
0

Faites vos propres chansons d'objets Song et pick-up de celui-ci au hasard

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(); 
     } 
    } 
} 
Questions connexes