2014-01-06 56 views
0

我想建立一個音樂播放器應用程序,我想以一首歌曲添加到playlist.Here添加一個功能是代碼:插入歌曲播放列表返回null

public static void addToPlaylist(ContentResolver resolver,SongDetails songDetails,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 + songDetails.getId())); 
    values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, songDetails.getId()); 
    values.put(MediaStore.Audio.Playlists.Members.DISPLAY_NAME, songDetails.getSongTitle()); 
    values.put(MediaStore.Audio.Playlists.Members.ARTIST, songDetails.getArtistName()); 
    values.put(MediaStore.Audio.Playlists.Members.DATA, songDetails.getSongData()); 

    Log.i("URI:",resolver.insert(uri, values)+""); 
} 

和日誌中我總是得到:

URI: null 

這意味着該歌曲不被添加到playlist.Does有人有,爲什麼不添加任何歌曲的想法?

+0

你不想在''PLAY_ORDER'基地+ 1'? –

+0

它也返回null – Khafaga

+0

你在哪裏設置了songDetails'id'? –

回答

4

我一直在使用這種方法,它爲我工作。

public static String addTracksToPlaylist(final long id, List<MediaData> tracks, final Context context) { 
    int count = getPlaylistSize(id, context); 
    ContentValues[] values = new ContentValues[tracks.size()]; 
    for (int i = 0; i < tracks.size(); i++) { 
     values[i] = new ContentValues(); 
     values[i].put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, i + count + 1); 
     values[i].put(MediaStore.Audio.Playlists.Members.AUDIO_ID, tracks.get(i).getId()); 
    } 
    Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", id); 
    ContentResolver resolver = context.getContentResolver(); 
    int num = resolver.bulkInsert(uri, values); 
    resolver.notifyChange(Uri.parse("content://media"), null); 
    return String.format(context.getString(R.string.ADDED_TO_PLAYLIST), num, context.getString(R.string.CURRENT)); 
} 

從播放列表中檢索曲目:

public static Cursor getTrackListFromPlaylist(Context context, long plid) { 
     String[] MEDIA_COLUMNS = new String[] { 
       MediaStore.Audio.Media.ARTIST, 
       MediaStore.Audio.Media.ARTIST_ID, 
       MediaStore.Audio.Media.TITLE, 
       MediaStore.Audio.Playlists.Members.AUDIO_ID, 
       MediaStore.Audio.Media.DURATION, 
       MediaStore.Audio.Media.DATA, 
       MINUTES + "||':'||" + SECONDS + " as " + FORMATTED_DURATION, 
       MediaStore.Audio.Media.ALBUM_ID, 
       MediaStore.Audio.Media.ALBUM, 
       MediaStore.Audio.Media._ID 
     }; 
     return query(context, MediaStore.Audio.Playlists.Members.getContentUri("external", plid), 
       MEDIA_COLUMNS, null, null, MediaStore.Audio.Playlists.Members.DEFAULT_SORT_ORDER); 
    } 
+0

如果我們說我們要使用這個代碼,如果我們沒有插入除了它的id之外的音軌的名字,我們將如何檢索給定當前播放列表的歌曲的名稱? – Khafaga

+0

查看更新的回答 – Autocrab

+0

好吧,在你插入音頻編號的代碼中,到目前爲止這麼好,但是如何獲取數據,標題和其他內容,而無需將它們插入到第一位呢? – Khafaga