2016-09-23 47 views
0

我想根據特定的播放列表獲取歌曲。我獲取所有歌曲的音頻編號,但沒有得到任何其他信息,如標題,藝術家,專輯等。我也試過 this這與我的問題是一樣的。
這是我怎麼添加歌曲到播放列表:找不到歌曲,同時從播放列表中獲得音頻ID

public static void addToPlaylist(ContentResolver resolver, long audioId, int playlistId) { 
    String[] cols = new String[] { 
      "count(*)" 
    }; 
    Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId); 
    Cursor cur = resolver.query(uri, cols, null, null, null); 
    cur.moveToFirst(); 
    final int base = cur.getInt(0); 
    cur.close(); 
    ContentValues values = new ContentValues(); 
    values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, Integer.valueOf(base +(int) audioId)); 
    values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audioId); 
    Uri u =resolver.insert(uri, values); 
    Log.d("ItemFragment", "Added Playlist Item: " + u + " for AUDIO_ID " + audioId+" and PLAYLIST_ID = "+playlistId); 

} 

,然後我檢索特定播放列表的歌曲是這樣的:

public ArrayList<AudioFile> getSongListFromPlayList() { 
    ArrayList<AudioFile> audioFiles = new ArrayList<>(); 
    String[] proj = { MediaStore.Audio.Playlists.Members.AUDIO_ID}; 
    Cursor musicCursor = musicResolver.query(MediaStore.Audio.Playlists.Members.getContentUri("external",id) 
      , proj, null, null, null); 
    int x = 0; 
    Log.d(TAG,"cursor count : "+musicCursor.getCount()); 
    if ((musicCursor != null) && (musicCursor.moveToFirst())) { 
     do { 
      long a_id = musicCursor.getLong(musicCursor.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID)); 
      Uri mediaContentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
      String[] projection = new String[]{MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM, 
        MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID}; 
      String selection = MediaStore.Audio.Media._ID + "=?"; 
      String[] selectionArgs = new String[]{"" + a_id}; //This is the id you are looking for 

      Cursor mediaCursor = musicResolver.query(mediaContentUri, projection, selection, selectionArgs, null); 
      Log.d(TAG,"second cursor count : "+mediaCursor.getCount()); 
      if (mediaCursor.getCount() > 0) { 
       mediaCursor.moveToPosition(0); 
       String title = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.TITLE)); 
       String album = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)); 
       String artist = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)); 
       String data = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.DATA)); 
       String duration = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.DURATION)); 
       long id = mediaCursor.getLong(mediaCursor.getColumnIndex(MediaStore.Audio.Media._ID)); 

       audioFiles.add(new AudioFile(id,title,artist,data,duration,x)); 
       x++; 
      } 
     }while (musicCursor.moveToNext()); 
    } 
    Log.d(TAG,"size : "+audioFiles.size()); 
    return audioFiles; 
} 

和調試時我得到日誌:

D/SecondActivity:遊標數:11
D/SecondActivity:第二遊標數:1
d/SecondActivity:第二光標計數:0
d/SecondActivity:第二光標計數:0
d/SecondActivity:第二光標計數:0
d/SecondActivity:第二光標計數:0
d/SecondActivity:第二光標計數:0
d/SecondActivity:第二光標計數:0
d/SecondActivity:第二光標計數:0
d/SecondActivity:第二光標計數:0
d/SecondActivity:第二光標計數:0
D/SecondActivity:第二個遊標數:0
D/SecondActivity:size:1

請大家幫忙。

+0

刪除我的評論 – Theo

回答

0

您需要枚舉光標並向前移動,直到達到最終。一旦你有你的音樂光標,使用這樣的東西循環它

   if(musicCursor!= null && musicCursor.moveToFirst()){ 
      for (musicCursor.moveToFirst(); !musicCursor.isAfterLast(); musicCursor.moveToNext()) { 
     // do your stuff here, as you cycle through your musicCursor you will read the next audio_id which then gets the relevant data 
      } 
     } 

for循環將枚舉遊標。移除moveto位置, 將uri帶到循環之外。如果你在這個類中有幾個方法,把它放在最前面,這樣它就可以用於所有的方法,但你只需定義一次。定義投影只需要在for循環之外進行一次。 我已編輯你的代碼有點

public ArrayList<AudioFile> getSongListFromPlayList() { 
ArrayList<AudioFile> audioFiles = new ArrayList<>(); 
String[] proj = { MediaStore.Audio.Playlists.Members.AUDIO_ID}; 
Cursor musicCursor = musicResolver.query(MediaStore.Audio.Playlists.Members.getContentUri("external",id) 
     , proj, null, null, null); 

     String[] projection = new String[]{MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ALBUM, 
       MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM_ID}; 
     String selection = MediaStore.Audio.Media._ID + "=?"; 
if ((musicCursor != null) && (musicCursor.moveToFirst())) { 
     for (musicCursor.moveToFirst(); !musicCursor.isAfterLast(); musicCursor.moveToNext()) { 
     long a_id = musicCursor.getLong(musicCursor.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID)); 

     String[] selectionArgs = new String[]{"" + a_id}; //This is the id you are looking for 
     Cursor mediaCursor = musicResolver.query(mediaContentUri, projection, selection, selectionArgs, null); 
      String title = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.TITLE)); 
      String album = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)); 
      String artist = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)); 
      String data = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.DATA)); 
      String duration = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.DURATION)); 
      long id = mediaCursor.getLong(mediaCursor.getColumnIndex(MediaStore.Audio.Media._ID)); 
      audioFiles.add(new AudioFile(id,title,artist,data,duration,x)); 

    } 
     } 


Log.d(TAG,"size : "+audioFiles.size()); 
return audioFiles; 
+0

我嘗試過,但沒有奏效 –

+0

等什麼接下來會發生?簡單地說它不起作用並不是真的有用。 – Theo

+0

其實這是我的錯。我在「addToPlaylist」方法中傳遞了錯誤的「id」。這不是我之前創建的任何播放列表。所以我改變了,代碼工作正常。謝謝你的幫助。 –

相關問題