Exemplo n.º 1
0
  /**
   * 获取一个mp3文件
   *
   * @param context
   * @param _id
   * @return
   */
  public static Mp3Info getMp3Info(Context context, long _id) {
    Cursor cursor =
        context
            .getContentResolver()
            .query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                null,
                MediaStore.Audio.Media._ID + "=" + _id,
                null,
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

    Mp3Info mp3Info = null;
    if (cursor.moveToNext()) {
      mp3Info = new Mp3Info();
      long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)); // 音乐id
      String title =
          cursor.getString((cursor.getColumnIndex(MediaStore.Audio.Media.TITLE))); // 音乐标题
      String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)); // 艺术家
      long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)); // 时长
      long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE)); // 文件大小
      String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)); // 文件路径
      int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC)); // 是否为音乐

      if (isMusic != 0) { // 只把音乐添加到集合当中
        mp3Info.setId(id);
        mp3Info.setTitle(title);
        mp3Info.setArtist(artist);
        mp3Info.setDuration(duration);
        mp3Info.setSize(size);
        mp3Info.setUrl(url);
      }
    }
    cursor.close();
    return mp3Info;
  }
Exemplo n.º 2
0
 /**
  * 获取多个
  *
  * @param context
  * @return
  */
 public static ArrayList<Mp3Info> getMp3Infos(Context context) {
   Cursor cursor =
       context
           .getContentResolver()
           .query(
               MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
               null,
               MediaStore.Audio.Media.DURATION + ">=180000",
               null,
               MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
   ArrayList<Mp3Info> mp3Infos = new ArrayList<Mp3Info>();
   for (int i = 0; i < cursor.getCount(); i++) {
     cursor.moveToNext();
     Mp3Info mp3Info = new Mp3Info();
     long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)); // 音乐id
     String title =
         cursor.getString((cursor.getColumnIndex(MediaStore.Audio.Media.TITLE))); // 音乐标题
     String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)); // 艺术家
     long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)); // 时长
     long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE)); // 文件大小
     String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)); // 文件路径
     int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC)); // 是否为音乐
     if (isMusic != 0) { // 只把音乐添加到集合当中
       mp3Info.setId(id);
       mp3Info.setTitle(title);
       mp3Info.setArtist(artist);
       mp3Info.setDuration(duration);
       mp3Info.setSize(size);
       mp3Info.setUrl(url);
       mp3Infos.add(mp3Info);
     }
   }
   return mp3Infos;
 }