如何在MusicBobber库中设置音乐文件

时间:2016-10-29 10:12:51

标签: android

我在我的项目中使用MusicBobber库。没有关于从sdcardraw设置音频uri的维基。

我知道在android mediaplayer中你可以这样设置音乐文件:

MediaPlayer mediaPlayer= MediaPlayer.create(this, R.raw.song);

但不知道这个特定的库。有没有人和这个图书馆合作过?我怎么能告诉它播放一首歌?

1 个答案:

答案 0 :(得分:1)

以下是在背景中获取歌曲的示例

ResultSet

这就是你如何使用arraylist对象

class MusicLoader extends BaseAsyncTaskLoader<Collection<MusicItem>> {

private final Uri albumArtUri = Uri.parse("content://media/external/audio/albumart");

public MusicLoader(Context context) {
    super(context);
}

@Override
public Collection<MusicItem> loadInBackground() {
    String[] projection = new String[]{
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.ALBUM_ID,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.DURATION,
            MediaStore.Audio.Media.DATA,
    };
    Cursor cursor = getContext().getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            projection,
            MediaStore.Audio.Media.IS_MUSIC + "=1",
            null,
            "LOWER(" + MediaStore.Audio.Media.ARTIST + ") ASC, " +
                    "LOWER(" + MediaStore.Audio.Media.ALBUM + ") ASC, " +
                    "LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC"
    );
    if (cursor == null) {
        return Collections.emptyList();
    }
    List<MusicItem> items = new ArrayList<>();
    try {
        if (cursor.moveToFirst()) {
            int title = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
            int album = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
            int artist = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
            int duration = cursor.getColumnIndex(MediaStore.Audio.Media.DURATION);
            int albumId = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);
            int data = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
            do {
                MusicItem item = new MusicItem()
                        .title(cursor.getString(title))
                        .album(cursor.getString(album))
                        .artist(cursor.getString(artist))
                        .duration(cursor.getLong(duration))
                        .albumArtUri(ContentUris.withAppendedId(albumArtUri, cursor.getLong(albumId)))
                        .fileUri(Uri.parse(cursor.getString(data)))
                        ;
                items.add(item);
            } while (cursor.moveToNext());
        }
    } finally {
        cursor.close();
    }
    return items;
}
}

如果你不想传递数组列表,你也可以制作一个getter和setter。 For more info