gpt4 book ai didi

android - 如何从专辑 ID 中获取歌曲和其他媒体?

转载 作者:太空狗 更新时间:2023-10-29 16:34:27 28 4
gpt4 key购买 nike

我想从专辑 ID 中获取歌曲和其他媒体详细信息。我只有专辑 ID,我尝试了很多解决方案,但没有一个成功。

我的代码 fragment :

ContentResolver contentResolver = getContentResolver();
Uri mediaUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, albumId);
Log.wtf("SKJDBKJ", mediaUri.toString());
Cursor mediaCursor = contentResolver.query(mediaUri, null, null, null, null);

// if the cursor is null.
if(mediaCursor != null && mediaCursor.moveToFirst())
{
Log.wtf("DSJK", "entered cursor");
//get Columns
int titleColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
int idColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media._ID);
int artistColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);

// Store the title, id and artist name in Song Array list.
do
{
long thisId = mediaCursor.getLong(idColumn);
String thisTitle = mediaCursor.getString(titleColumn);
String thisArtist = mediaCursor.getString(artistColumn);
Log.wtf("Title", thisTitle);

// Add the info to our array.
songArrayList.add(new Song(thisId, thisTitle, thisArtist));
}
while (mediaCursor.moveToNext());

// For best practices, close the cursor after use.
mediaCursor.close();
}

Log for mediaUri 返回当前相册的路径,例如: content://media/external/audio/media/41。有人告诉我该怎么做吗?

最佳答案

我自己做了。你可以在 contentResolver.query 上为 'selection' 字符串编写简单的 sqlite 查询。这个例子可以告诉你如何通过专辑 ID 获取歌曲。我认为这是最好的方法。

    ArrayList<Song> songs = new ArrayList<>();
String selection = "is_music != 0";

if (albumId > 0) {
selection = selection + " and album_id = " + albumId;
}

String[] projection = {
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ALBUM_ID
};
final String sortOrder = MediaStore.Audio.AudioColumns.TITLE + " COLLATE LOCALIZED ASC";

Cursor cursor = null;
try {
Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
cursor = getActivity().getContentResolver().query(uri, projection, selection, null, sortOrder);
if (cursor != null) {
cursor.moveToFirst();
int position = 1;
while (!cursor.isAfterLast()) {
Song song = new Song();
song.setTitle(cursor.getString(0));
song.setDuration(cursor.getLong(4));
song.setArtist(cursor.getString(1));
song.setPath(cursor.getString(2));
song.setPosition(position);
song.setAlbumId(cursor.getLong(6));

cursor.moveToNext();
}
}

} catch (Exception e) {
Log.e("Media", e.toString());
} finally {
if (cursor != null) {
cursor.close();
}
}

关于android - 如何从专辑 ID 中获取歌曲和其他媒体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32337949/

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