类android.provider.MediaStore.Audio.Media源码实例Demo

下面列出了怎么用android.provider.MediaStore.Audio.Media的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: APlayer   文件: MediaStoreUtil.java
/**
 * 根据歌手或者专辑id获取所有歌曲
 *
 * @param id 歌手id 专辑id
 * @param type 1:专辑  2:歌手
 * @return 对应所有歌曲的id
 */
public static List<Song> getSongsByArtistIdOrAlbumId(int id, int type) {
  String selection = null;
  String sortOrder = null;
  String[] selectionValues = null;
  if (type == Constants.ALBUM) {
    selection = MediaStore.Audio.Media.ALBUM_ID + "=?";
    selectionValues = new String[]{id + ""};
    sortOrder = SPUtil.getValue(mContext, SPUtil.SETTING_KEY.NAME,
        SPUtil.SETTING_KEY.CHILD_ALBUM_SONG_SORT_ORDER,
        SortOrder.ChildHolderSongSortOrder.SONG_A_Z);
  }
  if (type == Constants.ARTIST) {
    selection = MediaStore.Audio.Media.ARTIST_ID + "=?";
    selectionValues = new String[]{id + ""};
    sortOrder = SPUtil.getValue(mContext, SPUtil.SETTING_KEY.NAME,
        SPUtil.SETTING_KEY.CHILD_ARTIST_SONG_SORT_ORDER,
        SortOrder.ChildHolderSongSortOrder.SONG_A_Z);
  }
  return getSongs(selection, selectionValues, sortOrder);
}
 
源代码2 项目: APlayer   文件: MediaStoreUtil.java
/**
 * 根据文件夹名字
 */
public static List<Song> getSongsByParentId(int parentId) {
  List<Integer> ids = getSongIdsByParentId(parentId);

  if (ids.size() == 0) {
    return new ArrayList<>();
  }
  StringBuilder selection = new StringBuilder(127);
  selection.append(MediaStore.Audio.Media._ID + " in (");
  for (int i = 0; i < ids.size(); i++) {
    selection.append(ids.get(i)).append(i == ids.size() - 1 ? ") " : ",");
  }
  return getSongs(selection.toString(), null, SPUtil.getValue(mContext, SPUtil.SETTING_KEY.NAME,
      SPUtil.SETTING_KEY.CHILD_FOLDER_SONG_SORT_ORDER,
      SortOrder.ChildHolderSongSortOrder.SONG_A_Z));
}
 
源代码3 项目: APlayer   文件: MediaStoreUtil.java
public static List<Integer> getSongIds(@Nullable String selection, String[] selectionValues,
    String sortOrder) {
  if (!hasStoragePermissions()) {
    return new ArrayList<>();
  }

  List<Integer> ids = new ArrayList<>();
  try (Cursor cursor = makeSongCursor(selection, selectionValues, sortOrder)) {
    if (cursor != null && cursor.getCount() > 0) {
      while (cursor.moveToNext()) {
        ids.add(cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID)));
      }
    }
  } catch (Exception ignore) {

  }
  return ids;
}
 
源代码4 项目: dttv-android   文件: VideoUIFragment.java
private void MakeCursor() {
    String[] cols = new String[]{
            MediaStore.Video.Media._ID,
            MediaStore.Video.Media.TITLE,
            MediaStore.Video.Media.DATA,
            MediaStore.Video.Media.MIME_TYPE,
            MediaStore.Video.Media.ARTIST
    };
    ContentResolver resolver = getActivity().getContentResolver();
    if (resolver == null) {
        System.out.println("resolver = null");
    } else {
        StringBuffer select = new StringBuffer("");
        // 查询语句:检索出.mp3为后缀名,时长大于1分钟,文件大小大于1MB的媒体文件
		/*if(sp.getFilterSize()) {
			select.append(" and " + Media.SIZE + " > " + FILTER_SIZE);
		}*/
        if (settingUtil.isFilterVideo()) {
            select.append(" and " + Media.DURATION + " > " + Constant.FILTER_DURATION);
        }
        mSortOrder = MediaStore.Video.Media.TITLE + " COLLATE UNICODE";
        mWhereClause = MediaStore.Video.Media.TITLE + " != ''";
        mCursor = resolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                cols, mWhereClause + select, null, mSortOrder);
    }
}
 
源代码5 项目: APlayer   文件: MediaStoreUtil.java
public static List<Artist> getAllArtist() {
  if (!hasStoragePermissions()) {
    return new ArrayList<>();
  }
  ArrayList<Artist> artists = new ArrayList<>();
  try (Cursor cursor = mContext.getContentResolver()
      .query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
          new String[]{MediaStore.Audio.Media.ARTIST_ID,
              MediaStore.Audio.Media.ARTIST,
              "count(" + Media.ARTIST_ID + ")"},
          MediaStoreUtil.getBaseSelection() + ")" + " GROUP BY ("
              + MediaStore.Audio.Media.ARTIST_ID,
          null,
          SPUtil.getValue(mContext, SPUtil.SETTING_KEY.NAME, SPUtil.SETTING_KEY.ARTIST_SORT_ORDER,
              SortOrder.ArtistSortOrder.ARTIST_A_Z))) {
    if (cursor != null) {
      while (cursor.moveToNext()) {
        try {
          artists.add(new Artist(cursor.getInt(0),
              cursor.getString(1),
              cursor.getInt(2)));
        } catch (Exception ignored) {

        }
      }
    }
  }

  return artists;
}
 
源代码6 项目: APlayer   文件: MediaStoreUtil.java
public static List<Album> getAllAlbum() {
  if (!hasStoragePermissions()) {
    return new ArrayList<>();
  }
  ArrayList<Album> albums = new ArrayList<>();
  try (Cursor cursor = mContext.getContentResolver()
      .query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
          new String[]{MediaStore.Audio.Media.ALBUM_ID,
              MediaStore.Audio.Media.ALBUM,
              MediaStore.Audio.Media.ARTIST_ID,
              MediaStore.Audio.Media.ARTIST,
              "count(" + Media.ALBUM_ID + ")"},
          MediaStoreUtil.getBaseSelection() + ")" + " GROUP BY ("
              + MediaStore.Audio.Media.ALBUM_ID,
          null,
          SPUtil.getValue(mContext, SPUtil.SETTING_KEY.NAME, SPUtil.SETTING_KEY.ALBUM_SORT_ORDER,
              SortOrder.AlbumSortOrder.ALBUM_A_Z))) {
    if (cursor != null) {
      while (cursor.moveToNext()) {
        try {
          albums.add(new Album(cursor.getInt(0),
              cursor.getString(1),
              cursor.getInt(2),
              cursor.getString(3),
              cursor.getInt(4)));
        } catch (Exception ignored) {

        }

      }
    }
  }
  return albums;
}
 
源代码7 项目: APlayer   文件: MediaStoreUtil.java
public static List<Song> getLastAddedSong() {
  Calendar today = Calendar.getInstance();
  today.setTime(new Date());
  return getSongs(MediaStore.Audio.Media.DATE_ADDED + " >= ?",
      new String[]{String.valueOf((today.getTimeInMillis() / 1000 - (3600 * 24 * 7)))},
      MediaStore.Audio.Media.DATE_ADDED);
}
 
源代码8 项目: APlayer   文件: MediaStoreUtil.java
/**
 * 根据记录集获得歌曲详细
 *
 * @param cursor 记录集
 * @return 拼装后的歌曲信息
 */
@WorkerThread
public static Song getSongInfo(Cursor cursor) {
  if (cursor == null || cursor.getColumnCount() <= 0) {
    return Song.Companion.getEMPTY_SONG();
  }

  long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
  final String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
  final int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media._ID));

  Song song = new Song(
      id,
      processInfo(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)),
          TYPE_DISPLAYNAME),
      processInfo(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)),
          TYPE_SONG),
      processInfo(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)),
          TYPE_ALBUM),
      cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)),
      processInfo(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)),
          TYPE_ARTIST),
      cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID)),
      duration,
      Util.getTime(duration),
      data,
      cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE)),
      cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.YEAR)),
      cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE_KEY)),
      cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DATE_ADDED)));
  return song;
}
 
源代码9 项目: APlayer   文件: MediaStoreUtil.java
public static List<Song> getSongsByIds(List<Integer> ids) {
  List<Song> songs = new ArrayList<>();
  if (ids == null || ids.isEmpty()) {
    return songs;
  }
  StringBuilder selection = new StringBuilder(127);
  selection.append(MediaStore.Audio.Media._ID + " in (");
  for (int i = 0; i < ids.size(); i++) {
    selection.append(ids.get(i)).append(i == ids.size() - 1 ? ") " : ",");
  }

  return getSongs(selection.toString(), null);
}
 
源代码10 项目: APlayer   文件: MediaStoreUtil.java
/**
   * 删除歌曲
   *
   * @param data 删除参数 包括歌曲id、专辑id、艺术家id、播放列表id、parentId
   * @param type 删除类型 包括单个歌曲、专辑、艺术家、文件夹、播放列表
   * @return 是否歌曲数量
   */
  @Deprecated
  public static int delete(int data, int type, boolean deleteSource) {
    String where = null;
    String[] arg = null;

    //拼接参数
    switch (type) {
      case Constants.SONG:
        where = MediaStore.Audio.Media._ID + "=?";
        arg = new String[]{data + ""};
        break;
      case Constants.ALBUM:
      case Constants.ARTIST:
        if (type == Constants.ALBUM) {
          where = MediaStore.Audio.Media.ALBUM_ID + "=?";
          arg = new String[]{data + ""};
        } else {
          where = MediaStore.Audio.Media.ARTIST_ID + "=?";
          arg = new String[]{data + ""};
        }
        break;
      case Constants.FOLDER:
        List<Integer> ids = getSongIdsByParentId(data);
        StringBuilder selection = new StringBuilder(127);
//                for(int i = 0 ; i < ids.size();i++){
//                    selection.append(MediaStore.Audio.Media._ID).append(" = ").append(ids.get(i)).append(i != ids.size() - 1 ? " or " : " ");
//                }
        selection.append(MediaStore.Audio.Media._ID + " in (");
        for (int i = 0; i < ids.size(); i++) {
          selection.append(ids.get(i)).append(i == ids.size() - 1 ? ") " : ",");
        }
        where = selection.toString();
        arg = null;
        break;
    }

    return delete(getSongs(where, arg), deleteSource);
  }
 
源代码11 项目: APlayer   文件: MediaStoreUtil.java
/**
 * 删除指定歌曲
 */
@WorkerThread
public static int delete(List<Song> songs, boolean deleteSource) {
  //保存是否删除源文件
  SPUtil.putValue(App.getContext(), SPUtil.SETTING_KEY.NAME, SPUtil.SETTING_KEY.DELETE_SOURCE,
      deleteSource);
  if (songs == null || songs.size() == 0) {
    return 0;
  }

  //删除之前保存的所有移除歌曲id
  Set<String> deleteId = new HashSet<>(
      SPUtil.getStringSet(mContext, SPUtil.SETTING_KEY.NAME, SPUtil.SETTING_KEY.BLACKLIST_SONG));
  //保存到sp
  for (Song temp : songs) {
    if (temp != null) {
      deleteId.add(temp.getId() + "");
    }
  }
  SPUtil.putStringSet(mContext, SPUtil.SETTING_KEY.NAME, SPUtil.SETTING_KEY.BLACKLIST_SONG,
      deleteId);
  //从播放队列和全部歌曲移除
  MusicServiceRemote.deleteFromService(songs);

  DatabaseRepository.getInstance().deleteFromAllPlayList(songs).subscribe();

  //删除源文件
  if (deleteSource) {
    deleteSource(songs);
  }

  //刷新界面
  mContext.getContentResolver().notifyChange(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null);

  return songs.size();
}
 
源代码12 项目: APlayer   文件: MediaStoreUtil.java
/**
 * 删除源文件
 */
public static void deleteSource(List<Song> songs) {
  if (songs == null || songs.size() == 0) {
    return;
  }
  for (Song song : songs) {
    mContext.getContentResolver().delete(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        MediaStore.Audio.Media._ID + "=?", new String[]{song.getId() + ""});
    Util.deleteFileSafely(new File(song.getUrl()));
  }
}
 
源代码13 项目: APlayer   文件: MediaStoreUtil.java
/**
   * 过滤移出的歌曲以及铃声等
   */
  public static String getBaseSelection() {
//        Set<String> deleteId = SPUtil.getStringSet(mContext,SPUtil.SETTING_KEY.NAME,SPUtil.SETTING_KEY.BLACKLIST_SONG);
//        if(deleteId == null || deleteId.size() == 0)
//            return BASE_SELECTION;
//        StringBuilder stringBuilder = new StringBuilder();
//        stringBuilder.append(" and ");
//        int i = 0;
//        for (String id : deleteId) {
//            stringBuilder.append(MediaStore.Audio.Media._ID + " != ").append(id).append(i != deleteId.size() - 1 ?  " and " : " ");
//            i++;
//        }
//        return stringBuilder.toString();

    Set<String> deleteId = SPUtil
        .getStringSet(mContext, SPUtil.SETTING_KEY.NAME, SPUtil.SETTING_KEY.BLACKLIST_SONG);
    if (deleteId.size() == 0) {
      return MediaStore.Audio.Media.SIZE + ">" + SCAN_SIZE;
    }
    StringBuilder blacklist = new StringBuilder();
    blacklist.append(MediaStore.Audio.Media.SIZE + ">").append(SCAN_SIZE);
    blacklist.append(" and ");
    int i = 0;
    for (String id : deleteId) {
      if (i == 0) {
        blacklist.append(MediaStore.Audio.Media._ID).append(" not in (");
      }
      blacklist.append(id);
      blacklist.append(i != deleteId.size() - 1 ? "," : ")");
      i++;
    }
    return blacklist.append(BASE_SELECTION).toString();
  }
 
源代码14 项目: APlayer   文件: MediaStoreUtil.java
/**
 * 设置铃声
 */
public static void setRing(Context context, int audioId) {
  try {
    ContentValues cv = new ContentValues();
    cv.put(MediaStore.Audio.Media.IS_RINGTONE, true);
    cv.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
    cv.put(MediaStore.Audio.Media.IS_ALARM, false);
    cv.put(MediaStore.Audio.Media.IS_MUSIC, true);
    // 把需要设为铃声的歌曲更新铃声库
    if (mContext.getContentResolver().update(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, cv,
        MediaStore.MediaColumns._ID + "=?", new String[]{audioId + ""}) > 0) {
      Uri newUri = ContentUris
          .withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, audioId);
      RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newUri);
      ToastUtil.show(context, R.string.set_ringtone_success);
    } else {
      ToastUtil.show(context, R.string.set_ringtone_error);
    }
  } catch (Exception e) {
    //没有权限
    if (e instanceof SecurityException) {
      ToastUtil.show(context, R.string.please_give_write_settings_permission);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.System.canWrite(mContext)) {
          Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
          intent.setData(Uri.parse("package:" + mContext.getPackageName()));
          intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          if (Util.isIntentAvailable(mContext, intent)) {
            mContext.startActivity(intent);
          }
        }
      }
    }

  }
}
 
源代码15 项目: APlayer   文件: MediaStoreUtil.java
@Nullable
public static Cursor makeSongCursor(@Nullable String selection, final String[] selectionValues,
    final String sortOrder) {
  if (selection != null && !selection.trim().equals("")) {
    selection = getBaseSelection() + " AND " + selection;
  } else {
    selection = getBaseSelection();
  }
  try {
    return mContext.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        null, selection, selectionValues, sortOrder);
  } catch (SecurityException e) {
    return null;
  }
}
 
源代码16 项目: APlayer   文件: MediaStoreUtil.java
/**
 * 歌曲数量
 * @return
 */
public static int getCount() {
  try (Cursor cursor = mContext.getContentResolver()
      .query(Media.EXTERNAL_CONTENT_URI, new String[]{Media._ID}, getBaseSelection(), null, null)) {
    if (cursor != null) {
      return cursor.getCount();
    }
  } catch (Exception e) {
    Timber.v(e);
  }

  return 0;
}
 
源代码17 项目: AlarmOn   文件: MediaAlbumsView.java
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  super.onItemClick(parent, view, position, id);
  songsView.query(Media.EXTERNAL_CONTENT_URI, AlbumColumns.ALBUM_KEY + " = '" + getLastSelectedName() + "'");
  getFlipper().setInAnimation(getContext(), R.anim.slide_in_left);
  getFlipper().setOutAnimation(getContext(), R.anim.slide_out_left);
  getFlipper().showNext();
}
 
源代码18 项目: dttv-android   文件: VideoUIFragment.java
private void fillDataToListView() {
    playList = new ArrayList<String>();
    String[] fromColumns = new String[]{MediaStore.Video.Media.TITLE};
    int[] toLayoutIDs = new int[]{R.id.media_row_name};
    //Cursor cursor = readDataFromSD(getActivity());
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.dt_video_item, mCursor, fromColumns, toLayoutIDs, 0);

    video_listview.setAdapter(adapter);
    while (mCursor.moveToNext()) {
        //Log.i(TAG, "mine-type is:"+mCursor.getString(mCursor.getColumnIndex(MediaStore.Video.Media.MIME_TYPE)));
        playList.add(mCursor.getString(mCursor.getColumnIndex(MediaStore.Video.Media.DATA)));
    }
}
 
源代码19 项目: dttv-android   文件: AudioUIFragment.java
private Cursor readDataFromSD(Context context) {
      Log.d(TAG, "scanFile");
      String[] str = 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};
      StringBuffer select = new StringBuffer("");
      // 查询语句:检索出.mp3为后缀名,时长大于1分钟,文件大小大于1MB的媒体文件
/*if(sp.getFilterSize()) {
	select.append(" and " + Media.SIZE + " > " + FILTER_SIZE);
}*/
      if (settingUtil.isFilterAudio()) {
          select.append(" and " + Media.DURATION + " > " + Constant.FILTER_DURATION);
      }

/*if (!TextUtils.isEmpty(selections)) {
	select.append(selections);
}*/
      Cursor c = MusicUtils.query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
              str, MediaStore.Audio.Media.IS_MUSIC + "=1" + select,
              null, MediaStore.Audio.Media.TITLE_KEY);
      return c;
  }
 
源代码20 项目: dttv-android   文件: AudioUIFragment.java
private void initFillData() {
    playList = new ArrayList<String>();
    String[] fromColumns = new String[]{MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DATA};
    int[] toLayoutIDs = new int[]{R.id.media_row_name, R.id.media_row_artist, R.id.media_row_uri};
    mCursor = readDataFromSD(getActivity());
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.dt_audio_item, mCursor, fromColumns, toLayoutIDs, 0);
    audio_listview.setAdapter(adapter);
    while (mCursor.moveToNext()) {
        playList.add(mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
    }
    //mCursor.close();
}
 
源代码21 项目: APlayer   文件: MediaStoreUtil.java
/**
 * 根据路径获得歌曲id
 */
public static int getSongIdByUrl(String url) {
  return getSongId(MediaStore.Audio.Media.DATA + " = ?", new String[]{url});
}
 
源代码22 项目: APlayer   文件: MediaStoreUtil.java
public static Song getSongByUrl(String url) {
  return getSong(MediaStore.Audio.Media.DATA + " = ?", new String[]{url});
}
 
源代码23 项目: APlayer   文件: MediaStoreUtil.java
/**
 * 根据专辑id查询歌曲详细信息
 *
 * @param albumId 歌曲id
 * @return 对应歌曲信息
 */
public static Song getSongByAlbumId(int albumId) {
  return getSong(MediaStore.Audio.Media.ALBUM_ID + "=?", new String[]{albumId + ""});
}
 
源代码24 项目: APlayer   文件: MediaStoreUtil.java
/**
 * 根据歌曲id查询歌曲详细信息
 *
 * @param id 歌曲id
 * @return 对应歌曲信息
 */
public static Song getSongById(int id) {
  return getSong(MediaStore.Audio.Media._ID + "=?", new String[]{id + ""});
}
 
 类所在包
 同包方法