【发布时间】:2014-01-09 17:22:52
【问题描述】:
我正在使用 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
我编写了一个简单的应用程序/活动来测试它:
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);
}
}
此活动产生以下 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 的华为 Prism II 上测试并看到了相同的行为
为什么获取专辑艺术家的直接而不是“隐藏”的方式不起作用?
我期望专辑查询也返回“Various Artists”。
感谢所有帮助。
【问题讨论】:
-
到目前为止我实现的是一个自定义提供程序,它查询 MediaStore.Audio.Media 的专辑艺术家,并返回不同的 ALBUM_ID。它并不完美,性能也不尽如人意(因为我不能做 GROUP BY),但看起来它主要对我有用。如果有人知道更多关于为什么这不起作用,或者如何解决它,那就太好了。
标签: android android-contentprovider mediastore