2016-03-19 14 views
1

私はアンドロイドでシンプルなメディアプレーヤーアプリを開発しました。これは、SDカード内のすべての曲を読み込むためのリストビューを持って、バーと再生ボタンをシークします。私は自分のアプリのためのアルバムアートギャラリーを作成する必要があります(Googleの音楽アプリを再生するような)。私は、ソング名とアルバムアートを返すために、このコードを改善するための方法アンドロイドの音楽プレーヤーのサムネイル再生リストを作成するにはどうすればよいですか?

public ArrayList<String> GetFiles(String DirectoryPath) { 
     ArrayList<String> MyFiles = new ArrayList<String>(); 
     File f = new File(DirectoryPath); 
     f.mkdirs(); 
     File[] files = f.listFiles(); 
     if (files.length == 0) { 
      return null; 
     }else { 
      for (int i=0; i<files.length; i++) 
       MyFiles.add(files[i].getName()); 
     } 

     return MyFiles; 
    } 
  1. 、SDカードからすべての曲を取得するには、このコードを使用しましたか?

  2. 各曲のサムネイル表示の作成方法は?あなたは、コンテンツリゾルバを使用する必要が

    _________ 
    |   | 
    | image | 
    |   | 
    |_________| 
    | title | 
    |_________| 
    

答えて

0


Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
      // Perform a query on the content resolver. The URI we're passing specifies that we 
      // want to query for all audio media on external storage (e.g. SD card) 
      Cursor cur = contentResolver.query(uri, null, 
        MediaStore.Audio.Media.IS_MUSIC + " = 1", null, null); 
      if (cur == null) { 
       Log.e(TAG, "Failed to retrieve music: cursor is null :-("); 
       subscriber.onError(new Throwable("Failed to retrieve music: cursor is null :-(")); 
       return; 
      } 
      if (!cur.moveToFirst()) { 
       subscriber.onError(new Throwable("No results. :(Add some tracks!")); 

       Log.e(TAG, "Failed to move cursor to first row (no query results)."); 
       return; 
      } 
      int artistColumn = cur.getColumnIndex(MediaStore.Audio.Media.ARTIST); 
      int titleColumn = cur.getColumnIndex(MediaStore.Audio.Media.TITLE); 
      int albumColumn = cur.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID); 
      int durationColumn = cur.getColumnIndex(MediaStore.Audio.Media.DURATION); 
      int idColumn = cur.getColumnIndex(MediaStore.Audio.Media._ID); 
      int dataColumn = cur.getColumnIndex(MediaStore.Audio.Media.DATA); 
      ArrayList<LocalTrack> tracks = new ArrayList<>(); 

      do { 
       Log.i(TAG, "ID: " + cur.getString(idColumn) + " Title: " + cur.getString(titleColumn)); 
       tracks.add(new LocalTrack(
         cur.getLong(idColumn), 
         cur.getString(artistColumn), 
         cur.getString(titleColumn), 
         cur.getLong(albumColumn), 
         cur.getLong(durationColumn), 
         cur.getString(dataColumn))); 
      } while (cur.moveToNext()); 
      Log.i(TAG, "Done querying media. MusicRetriever is ready."); 
      cur.close(); 


      for (LocalTrack localTrack : tracks) { 
       localTrack.setArtPath(findAlbumArt(localTrack)); 
      } 
      subscriber.onNext(tracks); 

public String findAlbumArt(LocalTrack localTrack) { 
    Cursor cursor = contentResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, 
      new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART}, 
      MediaStore.Audio.Albums._ID + "=?", 
      new String[]{String.valueOf(localTrack.getAlbum())}, 
      null); 

    String path = null; 
    if (cursor != null) { 
     if (cursor.moveToFirst()) { 
      path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART)); 
      // do whatever you need to do 
      cursor.close(); 
     } 
    } 

    return path; 
} 
+0

ありがとうございます。私はそれを試してみます –

+0

あなたはすべてのプロジェクトを見たい場合:https://github.com/joxad/zikobot あなたが使用できる部分:https://github.com/joxad/zikobot/blob/master/ app/src/main/java/com/startogamu/zikobot/module/content_resolver/manager/LocalMusicManager.java ダガー2を使用していますが、あなたのニーズに合わせて更新できるはずです – Joxad

+0

ありがとうございます。私が試してみます –

関連する問題