从MediaStore.Audio.Playlists获取歌曲

时间:2012-07-02 10:46:33

标签: android playlist

在此代码中,我正在创建包含Id和名称的播放列表,但当我尝试显示已创建歌曲的歌曲时,它显示没有名为Media Store.audio.Playlists.Members.audio_Id的列,MediaStore.Audio.Playlists.Members.Tracks

如何从播放列表中提取歌曲?

public static Cursor query(context context, Uri uri, String[] projection,
              String selection, String[] selectionArgs, String sortOrder, 
                  int limit) 
{
    ContentResolver resolver = context.getContentResolver();
    if (resolver == null) {
        return null;
    }

    if (limit > 0) {
        uri = uri.buildUpon().appendQueryParameter("limit", "" + limit).build();
    }

    return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
} 

public static Cursor query(Context context, Uri uri, String[] projection,
          String selection, String[] selectionArgs, String sortOrder)
{

    String[] projection1 = new String[]{MediaStore.Audio.Playlists.Members.AUDIO_ID};

    return query(context, uri, projection1, selection, selectionArgs, sortOrder, 0);
}

private static int intFromCursor(Cursor c) {
    int id = -1;

    if (c != null) {
        c.moveToFirst();

        if (!c.isAfterLast()) {
            id = c.getInt(0);
        }
    }

    c.close();
    return id;
}

public static int idForplaylist(Context context, String name) {
    Cursor c = query(context, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
                            new String[] { MediaStore.Audio.Playlists._ID },
                            MediaStore.Audio.Playlists.NAME + "=?",
                            new String[] { name },
                            MediaStore.Audio.Playlists.NAME);
    return intFromCursor(c);
}

public static int idFortrack(Context context, String path) {
    Cursor c = query(context, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
                            new String[] { MediaStore.Audio.Media._ID },
                            MediaStore.Audio.Media.DATA + "=?",
                            new String[] { path },
                            MediaStore.Audio.Media.DATA);
    return intFromCursor(c);
}

public static void writePlaylist(Context context, String playlistName, ArrayList<String> paths) {
    Cursor cursor;
    ContentResolver resolver = context.getContentResolver();
    int playlistId = idForplaylist(context, playlistName);
    String[] cols = new String[]{ MediaStore.Audio.Playlists._ID };

    Uri  uri;
    if (playlistId == -1) { 
        ContentValues values = new ContentValues(1);
        values.put(MediaStore.Audio.Playlists.NAME, playlistName);
        uri = resolver.insert(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, values);
        playlistId = idForplaylist(context, playlistName);
    } else {
        uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId); 
    }

    Log.d(TAG, String.format("Writing playlist %s", uri));

    // Add all the new tracks to the playlist.
    int size = paths.size();
    ContentValues values [] = new ContentValues[size];

    for (int k = 0; k < size; ++k) {
        values[k] = new ContentValues();
        values[k].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, k);
        values[k].put(MediaStore.Audio.Playlists.Members.AUDIO_ID, idFortrack(context, paths.get(k)));
        //values[k].put(MediaStore.Audio.Playlists.Members.TRACK,idFortrack(context,paths.get(k)));
    }

    resolver.bulkInsert(uri, values);
    PlaylistName.setText("");
}

public void displayPlaylistSongs(long playlistId)
{
    String[] projection = new String[] { MediaStore.Audio.Playlists.Members.TRACK,
                                    MediaStore.Audio.Playlists.Members.ARTIST};

    final String[] columns = new String[]{MediaStore.Audio.Playlists.Members.TRACK};
    int[] to = new int[]{android.R.id.text1};

    try {
        Cursor c = getContentResolver().query(
                       MediaStore.Audio.Playlists.Members.getContentUri("external",                         
                        playlistId), projection, null, null, null);

        pAdapter = new SimpleCursorAdapter(getBaseContext(),
                            android.R.layout.simple_list_item_1, c,columns, to);
        list.setAdapter(pAdapter);

0 个答案:

没有答案