01.如何扫描本地音频或者视频

yangchong211 / 文 发表于2018-02-05 19:09 次阅读

目录介绍

  • 1.关于Content Providers的作用
  • 1.1 Content Providers作用介绍
  • 1.2 利用ContentResolver.query()读取数据
  • 1.3 将cursor中的数据转存到事先定义好的List中
  • 1.4 关于实体类介绍
  • 1.5 注意添加权限,还需要动态申请读取本地文件权限
  • 2.如何展示缩略图,歌手,专辑等信息
  • 2.1 获取缩略图
  • 3.关于其他介绍
  • 3.1 关于音视频案例和博客汇总介绍
  • 3.2 关于版本更新情况
  • 3.3 关于我的博客

1.关于Content Providers的作用

1.1 Content Providers作用介绍

  • Android通过Content Providers将公共数据类型(音频、视频、图像、联系人信息等)整合到数据库中,储存地址和基本信息。

1.2 利用ContentResolver.query()读取数据

  • 1.2.1 这里举的例子以搜索手机中音频为案例
public final Cursor query (Uri uri,                 //Url,即查询路径
                String[] projection,   //查询时希望获得的列,如果填null,则返回所有列
                String selection,      //查询时的条件,select语句中where用到,可填null
                String[] selectionArgs, //查询条件属性值
                String sortOrder,       //查询到的数据的默认排序,null则不进行排序
                )
  • 1.2.2 实际案例中的代码展示
Cursor cursor = context.getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                new String[]{
                        BaseColumns._ID,
                        MediaStore.Audio.AudioColumns.IS_MUSIC,
                        MediaStore.Audio.AudioColumns.TITLE,
                        MediaStore.Audio.AudioColumns.ARTIST,
                        MediaStore.Audio.AudioColumns.ALBUM,
                        MediaStore.Audio.AudioColumns.ALBUM_ID,
                        MediaStore.Audio.AudioColumns.DATA,
                        MediaStore.Audio.AudioColumns.DISPLAY_NAME,
                        MediaStore.Audio.AudioColumns.SIZE,
                        MediaStore.Audio.AudioColumns.DURATION
                },
                SELECTION,
                new String[]{String.valueOf(filterSize), String.valueOf(filterTime)},
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

1.3 将cursor中的数据转存到事先定义好的List中

  • 1.3.1 代码展示如下所示
List<LocalMusic> musicList = new ArrayList<>();
if (cursor == null) {
    return musicList;
}

int i = 0;
while (cursor.moveToNext()) {
    // 是否为音乐,魅族手机上始终为0
    int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.IS_MUSIC));
    if (!isFly() && isMusic == 0) {
        continue;
    }

    long id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));
    String title = cursor.getString((cursor.getColumnIndex(MediaStore.Audio.AudioColumns.TITLE)));
    String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST));
    String album = cursor.getString((cursor.getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM)));
    long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.ALBUM_ID));
    long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA));
    String fileName = cursor.getString((cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DISPLAY_NAME)));
    long fileSize = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));

    LocalMusic music = new LocalMusic();
    music.setId(id);
    music.setType(LocalMusic.Type.LOCAL);
    music.setTitle(title);
    music.setArtist(artist);
    music.setAlbum(album);
    music.setAlbumId(albumId);
    music.setDuration(duration);
    music.setPath(path);
    music.setFileName(fileName);
    music.setFileSize(fileSize);
    musicList.add(music);
}

1.4 关于实体类介绍

  • 关于实体类,可以直接参考我的demo案例,结合案例看博客会更加有效果
  • 我的案例地址:https://github.com/yangchong211/YCAudioPlayer

1.5 注意添加权限,还需要动态申请读取本地文件权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

2.如何展示缩略图,歌手,专辑等信息

2.1 获取缩略图

/**
 * 获取小图标
 * @param music             music
 * @return                  bitmap对象
 */
public Bitmap loadThumbnail(LocalMusic music) {
    return loadCover(music, Type.THUMBNAIL);
}

/**
 * 获取蒙层透明背景bitmap
 * @param music             music
 * @return                  bitmap对象
 */
public Bitmap loadBlur(LocalMusic music) {
    return loadCover(music, Type.BLUR);
}

/**
 * 获取蒙层透明背景bitmap
 * @param music             music
 * @return                  bitmap对象
 */
public Bitmap loadRound(LocalMusic music) {
    return loadCover(music, Type.ROUND);
}

private Bitmap loadCover(LocalMusic music, Type type) {
    Bitmap bitmap;
    String key = getKey(music, type);
    if (TextUtils.isEmpty(key)) {
        bitmap = mCoverCache.get(KEY_NULL.concat(type.value));
        if (bitmap != null) {
            return bitmap;
        }
        bitmap = getDefaultCover(type);
        mCoverCache.put(KEY_NULL.concat(type.value), bitmap);
        return bitmap;
    }
    bitmap = mCoverCache.get(key);
    if (bitmap != null) {
        return bitmap;
    }
    bitmap = loadCoverByType(music, type);
    if (bitmap != null) {
        mCoverCache.put(key, bitmap);
        return bitmap;
    }
    return loadCover(null, type);
}

private String getKey(LocalMusic music, Type type) {
    if (music == null) {
        return null;
    }
    if (music.getType() == LocalMusic.Type.LOCAL && music.getAlbumId() > 0) {
        return String.valueOf(music.getAlbumId()).concat(type.value);
    } else if (music.getType() == LocalMusic.Type.ONLINE && !TextUtils.isEmpty(music.getCoverPath())) {
        return music.getCoverPath().concat(type.value);
    } else {
        return null;
    }
}

/**
 * 获取默认的bitmap视图
 * @param type          类型
 * @return              bitmap对象
 */
private Bitmap getDefaultCover(Type type) {
    switch (type) {
        case BLUR:
            return BitmapFactory.decodeResource(Utils.getContext().getResources(), R.drawable.default_cover);
        case ROUND:
            Bitmap bitmap = BitmapFactory.decodeResource(Utils.getContext().getResources(), R.drawable.default_cover);
            bitmap = ImageUtils.resizeImage(bitmap, ScreenUtils.getScreenWidth() / 2, ScreenUtils.getScreenWidth() / 2);
            return bitmap;
        default:
            return BitmapFactory.decodeResource(Utils.getContext().getResources(), R.drawable.default_cover);
    }
}

private Bitmap loadCoverByType(LocalMusic music, Type type) {
    Bitmap bitmap;
    if (music.getType() == LocalMusic.Type.LOCAL) {
        bitmap = loadCoverFromMediaStore(music.getAlbumId());
    } else {
        bitmap = loadCoverFromFile(music.getCoverPath());
    }
    switch (type) {
        case BLUR:
            return ImageUtils.blur(bitmap);
        case ROUND:
            bitmap = ImageUtils.resizeImage(bitmap, ScreenUtils.getScreenWidth() / 2, ScreenUtils.getScreenWidth() / 2);
            return ImageUtils.createCircleImage(bitmap);
        default:
            return bitmap;
    }
}

/**
 * 从媒体库加载封面<br>
 * 本地音乐
 */
private Bitmap loadCoverFromMediaStore(long albumId) {
    ContentResolver resolver = Utils.getContext().getContentResolver();
    Uri uri = FileMusicUtils.getMediaStoreAlbumCoverUri(albumId);
    InputStream is;
    try {
        is = resolver.openInputStream(uri);
    } catch (FileNotFoundException ignored) {
        return null;
    }
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    return BitmapFactory.decodeStream(is, null, options);
}

/**
 * 从下载的图片加载封面<br>
 * 网络音乐
 */
private Bitmap loadCoverFromFile(String path) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    return BitmapFactory.decodeFile(path, options);
}

3.关于其他介绍

3.3 个人博客

收藏 赞 (0) 踩 (0)