gpt4 book ai didi

android - 无法在Android中使用MediaPlayer播放MediaStore.Audio歌曲

转载 作者:行者123 更新时间:2023-12-03 00:16:17 25 4
gpt4 key购买 nike

我目前正在制作一个简单的音乐播放器,并且设法通过下面的示例脚本从媒体获取音乐列表。但是,问题是我不知道如何播放ContentResolver中的音乐。我读过http://developer.android.com/reference/android/provider/MediaStore.Audio.html,但很难理解。有好的样本或教程吗?我要实现的是从Android中的ContentResolver播放音乐。

   public  Track(Cursor cursor)
{
id = cursor.getLong( cursor.getColumnIndex( MediaStore.Audio.Media._ID ));
path = cursor.getString( cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
title = cursor.getString( cursor.getColumnIndex( MediaStore.Audio.Media.TITLE ));
album = cursor.getString( cursor.getColumnIndex( MediaStore.Audio.Media.ALBUM ));
artist = cursor.getString( cursor.getColumnIndex( MediaStore.Audio.Media.ARTIST ));
albumId = cursor.getLong( cursor.getColumnIndex( MediaStore.Audio.Media.ALBUM_ID ));
artistId = cursor.getLong( cursor.getColumnIndex( MediaStore.Audio.Media.ARTIST_ID ));
duration = cursor.getLong( cursor.getColumnIndex( MediaStore.Audio.Media.DURATION ));
trackNo = cursor.getInt( cursor.getColumnIndex( MediaStore.Audio.Media.TRACK ));
uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
}

public static List getItems(Context activity) {

List tracks = new ArrayList();
ContentResolver resolver = activity.getContentResolver();
Cursor cursor = resolver.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
Track.COLUMNS,
null,
null,
null
);
while( cursor.moveToNext() ){
if( cursor.getLong(cursor.getColumnIndex( MediaStore.Audio.Media.DURATION)) < 3000 ){continue;}
tracks.add(new Track(cursor));
}
cursor.close();
return tracks;
}

最佳答案

这是Google提供的文档,我引用:

Another feature that may be useful in a media player application is the ability to retrieve music that the user has on the device. You can do that by querying the ContentResolver for external media:


ContentResolver contentResolver = getContentResolver();
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(uri, null, null, null, null);
if (cursor == null) {
// query failed, handle error.
} else if (!cursor.moveToFirst()) {
// no media on the device
} else {
int titleColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
do {
long thisId = cursor.getLong(idColumn);
String thisTitle = cursor.getString(titleColumn);
// ...process entry...
} while (cursor.moveToNext());
}

To use this with the MediaPlayer, you can do this:


long id = /* retrieve it from somewhere */;
Uri contentUri = ContentUris.withAppendedId(
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);

mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource(getApplicationContext(), contentUri);

// ...prepare and start...

资源:
Media Playback: Retrieving Media from a Content Resolver

关于android - 无法在Android中使用MediaPlayer播放MediaStore.Audio歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31063507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com