Android media媒体库分析之:调用系统媒体库完成指(2)

public CenterMusicInfo queryMusicById(long id) {
  Cursor cursor = mContext.getContentResolver().query(
    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
    new String[] { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ARTIST,
      MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.YEAR, MediaStore.Audio.Media.MIME_TYPE, MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DATE_MODIFIED}
    ,MediaStore.Audio.Media._ID+"=? and ("+ MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE
    +"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or "  + MediaStore.Audio.Media.MIME_TYPE+"=?)", new String[]{String.valueOf(id),"audio/mpeg","audio/mp4",
      "audio/x-wav","audio/amr","audio/amr-wb","application/ogg","audio/aac","audio/x-ms-wma"},MediaStore.Audio.Media.DATE_MODIFIED+" desc");
 
  CenterMusicInfo retMI = null;
  try{
   
   if (cursor.moveToFirst()) {
    retMI = new CenterMusicInfo();
    retMI.setId(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)));
    retMI.setFileName(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));//文件名
    retMI.setTitle(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));//歌曲名
    retMI.setDuration(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)));//时长
    retMI.setSinger(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));//歌手名
    retMI.setAlbum(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)));//专辑名
    retMI.setYear(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.YEAR)));//年代
    retMI.setMimeType(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.MIME_TYPE)));
    retMI.setFileSize(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE)));
    retMI.setFilePath(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
    retMI.setDateModified(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DATE_MODIFIED)));
   }
  }finally{
   if(cursor != null){
    cursor.close();
   }
  }
  return retMI;

}

public ArrayList<CenterMusicInfo> queryMusicList(int position,int limit) {
  Uri.Builder builder = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.buildUpon();
  builder.appendQueryParameter("limit", String.valueOf(limit));
  Uri uri = builder.build();
  Cursor cursor = mContext.getContentResolver().query(
    uri,
    new String[] { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ARTIST,
      MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.YEAR, MediaStore.Audio.Media.MIME_TYPE, MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DATE_MODIFIED}
    , MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE
    +"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or " + MediaStore.Audio.Media.MIME_TYPE+"=? or "  + MediaStore.Audio.Media.MIME_TYPE+"=?", new String[]{"audio/mpeg","audio/mp4",
      "audio/x-wav","audio/amr","audio/amr-wb","application/ogg","audio/aac","audio/x-ms-wma"},MediaStore.Audio.Media.DATE_MODIFIED+" desc");
 
  ArrayList<CenterMusicInfo> musicList = new ArrayList<CenterMusicInfo>();
  try{
   
   if (cursor.moveToPosition(position)) {
    do {
     CenterMusicInfo musicInfo = new CenterMusicInfo();
     musicInfo.setId(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)));
     musicInfo.setFileName(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));//文件名
     musicInfo.setTitle(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));//歌曲名
     musicInfo.setDuration(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)));//时长
     musicInfo.setSinger(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));//歌手名
     musicInfo.setAlbum(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)));//专辑名
     musicInfo.setYear(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.YEAR)));//年代
     musicInfo.setMimeType(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.MIME_TYPE)));
     musicInfo.setFileSize(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE)));
     musicInfo.setFilePath(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
     musicInfo.setDateModified(cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DATE_MODIFIED)));
     musicList.add(musicInfo);
    }while(cursor.moveToNext());
   }
  }finally{
   if(cursor != null){
    cursor.close();
   }
  }
 
  return musicList;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/444696f78e7065760fb41c712069e89f.html