2011-08-19 62 views
4

我正在使用簡單的音頻媒體播放器。我正在使用媒體商店獲取存儲在SD卡上的所有歌曲的信息。到現在爲止還挺好。一切工作正常。Android:使用媒體商店最近添加的歌曲列表

但我現在卡住了。如何使用媒體商店獲取最後添加的(最近添加的)歌曲?

問候, Niral

回答

1

我不知道,如果這是你在找什麼,但是當我在默認的Android音樂播放器源擡頭一看,我發現有一個「最近添加」媒體商店中的播放列表。它的ID在MediaStore.Audio.Playlists-1

編輯:

經過進一步研究,我發現-1只是表明,它不會在播放表中存在的價值。 您可以使用以下方法,而不是:

在查詢MediaStore.Audio.Media,將它添加到您的where子句條件:

MediaStore.Audio.Media.DATE_ADDED + ">" + (System.currentTimeMillis()/1000 - NUM_OF_DAYS);

NUM_OF_DAYS是指你的音頻文件是如何保存老在你的SD卡。

注意:從MediaStore.Audio.Media,不MediaStore.Audio.Playlist查詢。

+0

不適合我... – elgui

3

這是默認的音樂播放器的源Android 2.3的

private void playRecentlyAdded() { 
    // do a query for all songs added in the last X weeks 
    int X = MusicUtils.getIntPref(this, "numweeks", 2) * (3600 * 24 * 7); 
    final String[] ccols = new String[] { MediaStore.Audio.Media._ID}; 
    String where = MediaStore.MediaColumns.DATE_ADDED + ">" + (System.currentTimeMillis()/1000 - X); 
    Cursor cursor = MusicUtils.query(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
      ccols, where, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER); 

    if (cursor == null) { 
     // Todo: show a message 
     return; 
    } 
    try { 
     int len = cursor.getCount(); 
     long [] list = new long[len]; 
     for (int i = 0; i < len; i++) { 
      cursor.moveToNext(); 
      list[i] = cursor.getLong(0); 
     } 
     MusicUtils.playAll(this, list, 0); 
    } catch (SQLiteException ex) { 
    } finally { 
     cursor.close(); 
    } 
} 
1

在您的自定義列表,你保持添加一個整型字段「dateAdded」,並訪問使用

int dateAddedIndex = internalContentCursor.getColumnIndex(MediaStore.Audio.Media.DATE_ADDED); 
    if (dateAddedIndex != -1) { 
     songs.setDateAdded(externalContentCursor.getInt(externalContentCursor.getColumnIndex(MediaStore.Audio.Media.DATE_ADDED))); 
    } 

得到這樣的排序後,根據他們添加的時間列表

public static List<Songs> getTopRecentAdded(List<Songs> list) { 
      Collections.sort(list, new Comparator<Songs>() { 
       @Override 
       public int compare(Songs left, Songs right) { 
        return left.getDateAdded() - right.getDateAdded(); 
       } 
      }); 

      Collections.reverse(list); 
      return list; 
     } 

這將返回最後添加的包含歌曲的列表。