2015-12-15 45 views
0

我使用此代碼獲取MediaStore中的所有音樂,但不僅僅是音樂,我的列表中包含我設備中的所有音樂以及「facebook」或「skype」等應用程序的聲音。如何避免這種情況?如何在沒有應用程序歌曲的情況下列出MediaStore中的所有音樂

public void getSongList(){ 
    //query external audio 
    ContentResolver musicResolver = getContentResolver(); 
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
    Toast.makeText(PlayListActivity.this, musicUri.getPath(), Toast.LENGTH_LONG).show(); 
    Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null); 
    //iterate over results if valid 
    if(musicCursor!=null && musicCursor.moveToFirst()){ 
     //get columns 
     int titleColumn = musicCursor.getColumnIndex 
       (android.provider.MediaStore.Audio.Media.TITLE); 
     int idColumn = musicCursor.getColumnIndex 
       (android.provider.MediaStore.Audio.Media._ID); 
     int artistColumn = musicCursor.getColumnIndex 
       (android.provider.MediaStore.Audio.Media.ARTIST); 
     int albumColumn = musicCursor.getColumnIndex 
       (android.provider.MediaStore.Audio.Media.ALBUM_ID); 
     //add songs to list 
     Bitmap bmThumbnail; 
     do { 
      String path = musicCursor.getString(musicCursor.getColumnIndex(MediaStore.Audio.Media.DATA)); 
      long thisId = musicCursor.getLong(idColumn); 
      long thisAlbumId = musicCursor.getLong(albumColumn); 
      String thisTitle = musicCursor.getString(titleColumn); 
      String thisArtist = musicCursor.getString(artistColumn); 
      songList.add(new Song(thisId, thisAlbumId, thisTitle, thisArtist, path)); 
     } 
     while (musicCursor.moveToNext()); 
    } 
} 

回答

1

我不確定這件事,但我是用下面的方法做的。

public void getSongList(){ 
//query external audio 
ContentResolver musicResolver = getContentResolver(); 
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
Toast.makeText(PlayListActivity.this, musicUri.getPath(), Toast.LENGTH_LONG).show(); 
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null); 
//iterate over results if valid 
if(musicCursor!=null && musicCursor.moveToFirst()){ 
    //get columns 
    int titleColumn = musicCursor.getColumnIndex 
      (android.provider.MediaStore.Audio.Media.TITLE); 
    int idColumn = musicCursor.getColumnIndex 
      (android.provider.MediaStore.Audio.Media._ID); 
    int artistColumn = musicCursor.getColumnIndex 
      (android.provider.MediaStore.Audio.Media.ARTIST); 
    int albumColumn = musicCursor.getColumnIndex 
      (android.provider.MediaStore.Audio.Media.ALBUM_ID); 
    int songDuration = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DUration); 
    //add songs to list 
    Bitmap bmThumbnail; 
    do { 
     String path = musicCursor.getString(musicCursor.getColumnIndex(MediaStore.Audio.Media.DATA)); 
     long thisId = musicCursor.getLong(idColumn); 
     long thisAlbumId = musicCursor.getLong(albumColumn); 
     String thisTitle = musicCursor.getString(titleColumn); 
     String thisArtist = musicCursor.getString(artistColumn); 
     int dur =  Integer.parseInt(musicCursor.getString(songDuration)); 
     if(dur>5000){ 
     songList.add(new Song(thisId, thisAlbumId, thisTitle, thisArtist, path));} 
     } 
     while (musicCursor.moveToNext()); 
    } 
} 

另一種方法可能是用目錄來做也許吧?

+0

我會試試,非常感謝 –

相關問題