MediaPlayer构建音乐播放器,你所需知道的一切

本文详述如何使用MediaPlayer创建一个本地音乐播放器,包括获取本地音乐数据、使用PlayService执行播放任务、构建PlayManager作为UI和服务的中介、处理意外情况、实现远程控制和保活服务。此外,还介绍了如何处理音频焦点、锁屏控制和Notification。
摘要由CSDN通过智能技术生成

如何用MediaPlayer写一个正经的音乐播放器

先贴上示例项目的Github地址:

BePlayer

Demo应用Play Store地址:



BePlayer

按照以下顺序介绍如何用MediaPlayer去构建一个基础的本地音乐播放器。

  1. 获取本地音乐数据;
  2. 构建PlayService,来执行音乐播放任务;
  3. 构建一个UI与PlayService的中间层——PlayManager,用来处理媒体文件的Playback生命周期;
  4. 在PlayManager中,加入处理意外情况的方式,所谓意外情况,例如耳机拔出、接到电话、其他播放器播放音乐等;
  5. 实现远程控制与PlayService保活,例如Notification与锁屏控制;

以上是已经实现的部分,以后再逐渐完善的有:

  • 桌面Widget播放控件以及控制;
  • 自定义播放列表支持;
  • 视频播放支持;
  • 远端媒体播放支持;
  • 播放的可视化效果;
  • 歌词支持;
  • MediaCodec支持;

获取本地音乐数据

最快的获取本地音乐信息的方式,就是通过ContentProvider获取,我们先构建一个model类Song.java去表示音乐文件:

public class Song {
   
  private String title, titleKey, artist, artistKey,
    album, albumKey, displayName, mimeType, path;
  private int id, albumId, artistId, duration, size, year, track;
  private boolean isRingtone, isPodcast, isAlarm, isMusic, isNotification;

  //private File mCoverFile;

  private Album albumObj;

  public Song (Bundle bundle) {
    id = bundle.getInt(MediaStore.Audio.Media._ID);
    title = bundle.getString(MediaStore.Audio.Media.TITLE);
    titleKey = bundle.getString(MediaStore.Audio.Media.TITLE_KEY);
    artist = bundle.getString(MediaStore.Audio.Media.ARTIST);
    artistKey = bundle.getString(MediaStore.Audio.Media.ARTIST_KEY);
    //mComposer = bundle.getString(MediaStore.Audio.Media.COMPOSER);
    album = bundle.getString(MediaStore.Audio.Media.ALBUM);
    albumKey = bundle.getString(MediaStore.Audio.Media.ALBUM_KEY);
    displayName = bundle.getString(MediaStore.Audio.Media.DISPLAY_NAME);
    year = bundle.getInt(MediaStore.Audio.Media.YEAR);
    mimeType = bundle.getString(MediaStore.Audio.Media.MIME_TYPE);
    path = bundle.getString(MediaStore.Audio.Media.DATA);

    artistId = bundle.getInt(MediaStore.Audio.Media.ARTIST_ID);
    albumId = bundle.getInt(MediaStore.Audio.Media.ALBUM_ID);
    track = bundle.getInt(MediaStore.Audio.Media.TRACK);
    duration = bundle.getInt(MediaStore.Audio.Media.DURATION);
    size = bundle.getInt(MediaStore.Audio.Media.SIZE);
    isRingtone = bundle.getInt(MediaStore.Audio.Media.IS_RINGTONE) == 1;
    isPodcast = bundle.getInt(MediaStore.Audio.Media.IS_PODCAST) == 1;
    isAlarm = bundle.getInt(MediaStore.Audio.Media.IS_ALARM) == 1;
    isMusic = bundle.getInt(MediaStore.Audio.Media.IS_MUSIC) == 1;
    isNotification = bundle.getInt(MediaStore.Audio.Media.IS_NOTIFICATION) == 1;
  }

然后从ContentProvider中获得手机上的音乐文件:

public static List<Song> getAudioList(Context context) {


  ContentResolver resolver = context.getContentResolver();
  Cursor cursor = resolver.query(
    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
    AUDIO_KEYS,
    MediaStore.Audio.Media.IS_MUSIC + "=" + 1,
    null,
    null);
  return getAudioList(cursor);
}

private static List<Song> getAudioList (Cursor cursor) {
  List<Song> audioList = null;
  if (cursor.getCount() > 0) {
    audioList = new ArrayList<Song>();
    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
      Bundle bundle = new Bundle ();
      for (int i = 0; i < AUDIO_KEYS.length; i++) {
        final String key = AUDIO_KEYS[i];
        final int columnIndex = cursor.getColumnIndex(key);
        final int type = cursor.getType(columnIndex);
        switch (type) {
          case Cursor.FIELD_TYPE_BLOB:
            break;
          case Cursor.FIELD_TYPE_FLOAT:
            float floatValue = cursor.getFloat(columnIndex);
            bundle.putFloat(key, floatValue);
            break;
          case Cursor.FIELD_TYPE_INTEGER:
            int intValue = cursor.getInt(columnIndex);
            bundle.putInt(key, intValue);
            break;
          case Cursor.FIELD_TYPE_NULL:
            break;
          case Cursor.FIELD_TYPE_STRING:
            String strValue = cursor.getString(columnIndex);
            bundle.putString(key, strValue);
            break;
        }
      }
      Song audio = new Song(bund
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值