2010-04-22 40 views
7

Có thể tìm thấy tên danh sách phát theo truy vấn trên MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI và sau đó xem cột MediaStore.Audio.PlaylistsColumns.NAME. Có một cột dữ liệu, MediaStore.Audio.PlaylistsColumns._DATA, nhưng nó trả về null.Đặt tên cho danh sách phát nhạc trên Android, cách một người có thể tìm thấy các bài hát trong danh sách phát?

Danh sách bài hát (MediaStore.Audio.Media.EXTERNAL_CONTENT_URI/id) dường như không hiển thị bất kỳ liên kết danh sách phát nào.

Trả lời

6

Đây là một chút từ chương trình của tôi:

Ý chính là bạn cần id cho danh sách phát để lấy các bài hát.

Về cơ bản, bạn có thể lấy mã của tôi và thay đổi nơi tuyên bố .name + "= '+ tên +"'",

private Cursor getPlaylists(String playlistId){ 
     Cursor cursor = null; 

String[] projection1 = { 
     MediaStore.Audio.Playlists._ID, 
     MediaStore.Audio.Playlists.NAME 
    }; 

    cursor = this.managedQuery(
     MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, 
     projection1, 
     MediaStore.Audio.Playlists._ID+ " = "+playlistId+"", 
     null, 
     null); 
startManagingCursor(cursor); 
cursor.moveToFirst(); 
    playlist_id = cursor.getString(0); 
    playlist_id2 = cursor.getLong(0); 

      if(playlist_id2 > 0){ 
      String[] projection = { 
        MediaStore.Audio.Playlists.Members.AUDIO_ID, 
        MediaStore.Audio.Playlists.Members.ARTIST, 
        MediaStore.Audio.Playlists.Members.TITLE, 
        MediaStore.Audio.Playlists.Members._ID 



       }; 
      cursor = null; 
       cursor = this.managedQuery(
        MediaStore.Audio.Playlists.Members.getContentUri("external",playlist_id2), 
        projection, 
        MediaStore.Audio.Media.IS_MUSIC +" != 0 ", 
        null, 
        null); 

      } 
      cManager(cursor,2,1); 
      return cursor; 
     } 
+0

BTW lý do tôi nhận được id là tôi kiểm tra lại chuỗi ID của mình để đảm bảo rằng chuỗi đó đúng và biến thành một khoảng thời gian dài. – shaneburgess

+3

cManager là gì? –

0

đây là cách để có được tất cả các danh sách nhạc từ thiết bị

public ArrayList<String> getPlayList() { 

      ArrayList<String> arrayList=new ArrayList<String>(); 

      String[] proj = {"*"}; 
      Uri tempPlaylistURI = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI; 

      // In the next line 'this' points to current Activity. 
      // If you want to use the same code in other java file then activity, 
      // then use an instance of any activity in place of 'this'. 

      Cursor playListCursor= activity.managedQuery(tempPlaylistURI, proj, null,null,null); 

      if(playListCursor == null){ 
       System.out.println("Not having any Playlist on phone --------------"); 
       return arrayList;//don't have list on phone 
      } 

      System.gc(); 

      String playListName = null; 

      System.out.println(">>>>>>> CREATING AND DISPLAYING LIST OF ALL CREATED PLAYLIST <<<<<<"); 

      for(int i = 0; i <playListCursor.getCount() ; i++) 
      { 
       playListCursor.moveToPosition(i); 
       playListName = playListCursor.getString(playListCursor.getColumnIndex("name")); 
       System.out.println("> " + i + " : " + playListName); 
       arrayList.add(playListName); 
      } 

      if(playListCursor != null) 
       playListCursor.close(); 

      return arrayList; 



     } 
+0

Câu hỏi đặt ra là truy xuất các bài hát trong danh sách phát, không phải về việc truy xuất tất cả danh sách phát! – akshay7692

0

Đây là cách làm việc để tải tuyến đường từ danh sách phát. Về cơ bản, nó lặp con trỏ thông qua tất cả truy vấn danh sách phát và mỗi lần nhận được id của thành viên (bản nhạc) và sử dụng id đó của bản nhạc, chúng tôi có thể lấy dữ liệu khác như đường dẫn, nghệ sĩ, thời lượng, album, v.v.

ContentResolver contentResolver = getContentResolver(); 
    Uri playListUri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistID); //playlistID is the _ID of the given playlist 
    MediaMetadataRetriever mr = new MediaMetadataRetriever(); 
    Cursor cursor = contentResolver.query(playListUri, null, null, null, null); 
    if(cursor != null) 
    { 
     if(cursor.moveToNext()) { 
      do { 
       String track_id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID)); 
       Uri mediaContentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
       String[] trackProjection = new String[]{MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.DATA}; 
       String selection = MediaStore.Audio.Media._ID + "=?"; 
       String[] selectionArgs = new String[]{"" + track_id}; 
       Cursor mediaCursor = contentResolver.query(mediaContentUri, trackProjection, selection, selectionArgs, null); 
       if (mediaCursor != null) { 
        if (mediaCursor.getCount() >= 0) { 
         mediaCursor.moveToPosition(0); 
         String song_title = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.TITLE)); 
         String song_artist = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)); 
         String song_album = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)); 
         String song_path = mediaCursor.getString(mediaCursor.getColumnIndex(MediaStore.Audio.Media.DATA)); 
        } 
       } 
      } while (cursor.moveToNext()); 
     } 

    } 
Các vấn đề liên quan