gpt4 book ai didi

android - 为什么 MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI 不提供准确的 "Album Artist"?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:45:07 26 4
gpt4 key购买 nike

我正在使用 android MediaStore。我可以获得设备上的音乐列表,以及与每个媒体项目相关的所有详细信息(标题、艺术家、持续时间等)

我希望能够显示专辑列表,以及他们的专辑艺术家。

需要明确的是,每首轨道都有一位艺术家,例如“NoFX”,但如果该轨道出现在合辑 CD 上,例如 Punk-O-Rama,则艺术家是“NoFX”,但专辑艺术家可能类似于“各种艺术家。”

我在这里复习了这个问题:

Android - Getting Album Artist from Cursor

我正在尝试使用 MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI 和 MediaStore.Audio.Albums.ARTIST 来实现。然而,这似乎没有返回正确的结果。

查看 Android 源代码,我可以看到实际上 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI 后面有一个 album_artist 字段,但它被标记为“@hide”。(参见 https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/provider/MediaStore.java 处的 ~1170 行)。

我在查看其他 [相关] Android 源代码时发现了这个字段: https://android.googlesource.com/platform/development/+/master/apps/Development/src/com/android/development/MediaScannerActivity.java

我编写了一个简单的应用程序/Activity 来对此进行测试:

package com.ma.albumartisttest;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.util.SparseArray;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// query all media and get the album_artist field
Cursor cursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Media.ALBUM_ID, "album_artist"}, null, null, null);

// Store an id=>name map
SparseArray<String> albumArtistNames = new SparseArray<String>();
if (cursor != null) {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
String mediaAlbumArtist = cursor.getString(1);
if (mediaAlbumArtist != null && mediaAlbumArtist.toLowerCase().contains("various")) {
// loop through the cursor, save "album_artist"s that look like "various"
albumArtistNames.put((int) cursor.getLong(0), mediaAlbumArtist);
}
}
cursor.close();
}

String msg;
if (albumArtistNames.size() == 0) {
msg = "No 'various' artists found!";
Log.d("TESTALBUMARTIST", msg);
} else {
StringBuffer out = new StringBuffer();
for (int i = 0; i < albumArtistNames.size(); i++) {
// loop through the albums found above
int albumId = albumArtistNames.keyAt(i);
String album_artist = albumArtistNames.get(albumId);
Cursor albumCursor = getContentResolver().query(
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ARTIST},
MediaStore.Audio.Albums._ID + "=?",
new String[]{"" + albumId}, null);
if (albumCursor != null) {
if (albumCursor.moveToFirst()) {
// print out what was found.
String artistFromAlbumsDB = albumCursor.getString(1);
Log.d("TESTALBUMARTIST", album_artist + ":" + artistFromAlbumsDB);
out.append("Album id: " + albumId).append('\n')
.append("Artist from media table: " + album_artist).append('\n')
.append("Artist from albums table: " + artistFromAlbumsDB)
.append("\n\n");
}
albumCursor.close();
}
}
msg = out.toString();
}

// show the results on-screen
TextView tv = new TextView(this);
tv.setText(msg);
setContentView(tv);
}
}

此 Activity 产生以下 logcat 输出:

12-20 14:16:45.688  22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists:Mike Garson
12-20 14:16:45.708 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists:Franco Corelli
12-20 14:16:45.728 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists:The Foreshadowing
12-20 14:16:45.748 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists - Polyvinyl Record Co:Volcano, I'm Still Excited!!
12-20 14:16:45.778 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists - Curve Music:GRAND:PM
12-20 14:16:45.808 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists - DiN:Ian Boddy
12-20 14:16:45.828 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists:SNFU
12-20 14:16:45.858 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists:Pulley
12-20 14:16:45.878 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists:Osker
12-20 14:16:45.908 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists:Pennywise
12-20 14:16:45.938 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists:NoFX
12-20 14:16:45.958 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists:Death By Stereo
12-20 14:16:45.978 22009-22009/com.ma.albumartisttest D/TESTALBUMARTIST﹕ Various Artists (Sillage Intemporel):Sheri Malckin

我在运行 4.4.2 的 Nexus 7 上运行它。我还在运行 4.1.1 的 Huawei Prism II 上测试并看到了相同的行为

为什么获取专辑艺术家的直接而不是“隐藏”的方法不起作用?

我期望专辑查询也返回“Various Artists”。

感谢所有帮助。

Visual output of test Application/Activity

最佳答案

我没有看到您在代码中的任何地方定义或导入 ListActivity。

看看这两个例子:

关于android - 为什么 MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI 不提供准确的 "Album Artist"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20710542/

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