Retrieving Media from a Content Resolver

来源:互联网 发布:淘宝账号被永久封号 编辑:程序博客网 时间:2024/05/21 19:24

在设备中查找音乐:

ContentResolver contentResolver= getContentResolver();
Uri uri= android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor= contentResolver.query(uri,null, null, null, null);
if(cursor ==null) {
   
// query failed, handle error.
} else if(!cursor.moveToFirst()){
   
// no media on the device
} else {
   
int titleColumn= cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
   
int idColumn= cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
   
do{
       
long thisId= cursor.getLong(idColumn);
       
String thisTitle= cursor.getString(titleColumn);
       
// ...process entry...
   
}while (cursor.moveToNext());
}

在MediaPlayer中使用它:

long id = /* retrieve it from somewhere */;Uri contentUri = ContentUris.withAppendedId(        android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);mMediaPlayer = new MediaPlayer();mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);mMediaPlayer.setDataSource(getApplicationContext(), contentUri);// ...prepare and start...

 

原创粉丝点击