• 【Android开发经验】如何获取媒体库中所有音频文件信息,并在文件增删后及时更新媒体库(转)


    转载请注明出处: http://blog.csdn.net/zhaokaiqiang1992

    今天一打开博客,发现一位朋友给我发了一封私信,请教下面的问题,所以特写此文章,为其解惑。

    从这位朋友的提问中,我们需要了解下面的这些问题,才能给他很好的解答:

    (1)如何获取手机里所有歌曲的信息?

    (2)在歌曲文件发生改变,比如增删操作之后,如何及时的更新媒体库,从而获取到最新的歌曲信息?

    (3)在4.4版本之后,扫描sd卡,更新媒体库的操作发生变化了吗?

    下面,我将就以上三个问题,进行解答。

    (1)如何获取手机里所有歌曲的信息?

    如果要解决这个问题,那么我们首先要知道在Android系统中,是如何对歌曲信息进行管理的。

    在Android中,系统为多媒体类型的文件(比如图片、音频、视频等)建立了数据库(sqlite数据库),从而完成多媒体数据的维护工作。我们当然可以不用这些系统的数据库,比如说,如果我们想获取所有歌曲,我们可以扫描sd上所有的文件夹中的文件,然后根据文件的后缀名,就可以取到我们想要的mp3、wma文件等。但是,这样的操作是非常效率低下的,所以是行不通的。

    Android系统为我们建立起多媒体数据库之后,便把多媒体常用的信息,比如歌曲名、文件大小、播放时长、专辑、歌手等常用信息保存在了数据库里,那我们可以直接用多媒体库中的数据,完成这个需求。虽然我们需要用多媒体库,但是我们不能直接操作。Android为这些常用的需要共享的数据(多媒体和联系人等),创建了ContentProvider,因此,如果我们想获取到这些信息,我们就需要用ContentProvider。

    在开始介绍之前,先给出需要用到的歌曲的实体类

    /**
     * 
     * @ClassName: com.example.mediastore.Song
     * @Description: 歌曲实体类
     * @author zhaokaiqiang
     * @date 2014-12-4 上午11:49:59
     * 
     */
    public class Song {
    
      private String fileName;
      private String title;
      private int duration;
      private String singer;
      private String album;
      private String year;
      private String type;
      private String size;
      private String fileUrl;
    
      public String getFileName() {
        return fileName;
      }
    
      public void setFileName(String fileName) {
        this.fileName = fileName;
      }
    
      public String getTitle() {
        return title;
      }
    
      public void setTitle(String title) {
        this.title = title;
      }
    
      public int getDuration() {
        return duration;
      }
    
      public void setDuration(int duration) {
        this.duration = duration;
      }
    
      public String getSinger() {
        return singer;
      }
    
      public void setSinger(String singer) {
        this.singer = singer;
      }
    
      public String getAlbum() {
        return album;
      }
    
      public void setAlbum(String album) {
        this.album = album;
      }
    
      public String getYear() {
        return year;
      }
    
      public void setYear(String year) {
        this.year = year;
      }
    
      public String getType() {
        return type;
      }
    
      public void setType(String type) {
        this.type = type;
      }
    
      public String getSize() {
        return size;
      }
    
      public void setSize(String size) {
        this.size = size;
      }
    
      public String getFileUrl() {
        return fileUrl;
      }
    
      public void setFileUrl(String fileUrl) {
        this.fileUrl = fileUrl;
      }
    
      public Song() {
        super();
      }
    
      public Song(String fileName, String title, int duration, String singer,
          String album, String year, String type, String size, String fileUrl) {
        super();
        this.fileName = fileName;
        this.title = title;
        this.duration = duration;
        this.singer = singer;
        this.album = album;
        this.year = year;
        this.type = type;
        this.size = size;
        this.fileUrl = fileUrl;
      }
    
      @Override
      public String toString() {
        return "Song [fileName=" + fileName + ", title=" + title
            + ", duration=" + duration + ", singer=" + singer + ", album="
            + album + ", year=" + year + ", type=" + type + ", size="
            + size + ", fileUrl=" + fileUrl + "]";
      }
    
    }

    有了上面的这些信息,我们完全可以做一个播放器了!

    有了实体类之后,我封装了一个类,专门用来获取歌曲信息,下面是实现的代码

    /**
     * 
     * @ClassName: com.example.mediastore.AudioUtils
     * @Description: 音频文件帮助类
     * @author zhaokaiqiang
     * @date 2014-12-4 上午11:39:45
     * 
     */
    public class AudioUtils {
    
      /**
       * 获取sd卡所有的音乐文件
       * 
       * @return
       * @throws Exception
       */
      public static ArrayList<Song> getAllSongs(Context context) {
    
        ArrayList<Song> songs = null;
    
        Cursor cursor = context.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.MIME_TYPE + "=? or "
                + MediaStore.Audio.Media.MIME_TYPE + "=?",
            new String[] { "audio/mpeg", "audio/x-ms-wma" }, null);
    
        songs = new ArrayList<Song>();
    
        if (cursor.moveToFirst()) {
    
          Song song = null;
    
          do {
            song = new Song();
            // 文件名
            song.setFileName(cursor.getString(1));
            // 歌曲名
            song.setTitle(cursor.getString(2));
            // 时长
            song.setDuration(cursor.getInt(3));
            // 歌手名
            song.setSinger(cursor.getString(4));
            // 专辑名
            song.setAlbum(cursor.getString(5));
            // 年代
            if (cursor.getString(6) != null) {
              song.setYear(cursor.getString(6));
            } else {
              song.setYear("未知");
            }
            // 歌曲格式
            if ("audio/mpeg".equals(cursor.getString(7).trim())) {
              song.setType("mp3");
            } else if ("audio/x-ms-wma".equals(cursor.getString(7).trim())) {
              song.setType("wma");
            }
            // 文件大小
            if (cursor.getString(8) != null) {
              float size = cursor.getInt(8) / 1024f / 1024f;
              song.setSize((size + "").substring(0, 4) + "M");
            } else {
              song.setSize("未知");
            }
            // 文件路径
            if (cursor.getString(9) != null) {
              song.setFileUrl(cursor.getString(9));
            }
            songs.add(song);
          } while (cursor.moveToNext());
    
          cursor.close();
    
        }
        return songs;
      }
    
    }

    代码的思路很简单,我们需要根据ContentResover获取到一个Cursor,然后根据这个游标,遍历所有的歌曲的信息。在上面的代码中,我们查询出了包括歌名、路径、文件大小等在内的共10项数据,对于一般的应用这些足够了。查询出来之后,我们把信息转换成了实体类,这样操作起来更加方便。

    如果要使用这个工具类,记得添加权限 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    (2)如何及时更新媒体库

    Android系统刷新媒体库的数据的时机,是在开机的时候,即手机一开机,系统便重新扫描一下sd卡,并将多媒体数据库更新一下。如果用户删除了某一个音频文件,不重新开机的话,数据库中的数据是不会更新的。那么,如果我们想用户一打开软件,就强制的更新多媒体数据库,应该怎么做呢?

    在4.4版本之前,我们可以使用发送广播的方式,强制刷新多媒体库

    IntentFilter intentFilter = new IntentFilter(
            Intent.ACTION_MEDIA_SCANNER_STARTED);
        intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
        intentFilter.addDataScheme("file");
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
            Uri.parse("file://"
                + Environment.getExternalStorageDirectory()
                    .getAbsolutePath())));

    发送广播之后,还需要注册一个广播接受者,来接受并处理扫描开始和结束事件

    private class ScanReceiver extends BroadcastReceiver {
        public void onReceive(Context context, Intent intent) {
          String action = intent.getAction();
          // 当系统开始扫描sd卡时,为了用户体验,可以加上一个等待框
          if (Intent.ACTION_MEDIA_SCANNER_STARTED.equals(action)) {
          }
          // 当系统扫描完毕时,停止显示等待框,并重新查询ContentProvider
          if (Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action)) {
          }
        }
      }

    通过这种方式,我们便可以强制更新媒体库。

    但是,在4.4之后,Android对一些操作的权限提高,如果在4.4的系统上使用这种方式,便会出现下面的错误

    Caused by: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=22360, uid=10163

    这是因为在4.4之后,这个广播只有系统应用才能发出,因此,我们不能使用这种方式了,我们可以使用下面的代码实现相同的功能:

    MediaScannerConnection.scanFile(this, new String[] { Environment
            .getExternalStorageDirectory().getAbsolutePath() }, null, null);
    使用MediaScannerConnection的scanFile方法,就可以强制扫描我们需要更新的文件路径,之后媒体数据库也会同步更新,这样,就不会出现文件删除,在媒体库中却能搜索到的情况了,也能解决这位朋友提出的新增加歌曲的信息获取问题了。
  • 相关阅读:
    NOIP2009 pj
    数星星(POJ2352 star)
    洛谷 p3372 模板-线段树 1
    Luogu P1198 [JSOI2008]最大数 线段树
    Bestcoder#92&HDU 6017 T3 Girl loves 233 DP
    NOIP2008pj & luoguP1058 立体图 模拟
    NOIP2003TG 加分二叉树 区间DP
    Redis Jedis lua脚本
    Mac Ideal 常用快捷键
    Mysql慢查询explain
  • 原文地址:https://www.cnblogs.com/li-fei/p/4320089.html
Copyright © 2020-2023  润新知