gpt4 book ai didi

从播放列表中检索时的 Android 歌曲 ID 与我从媒体中检索时的不同

转载 作者:行者123 更新时间:2023-11-29 21:05:54 29 4
gpt4 key购买 nike

我有 2 种不同的方法来使用内容解析器检索歌曲。一个用于获取手机上的所有歌曲,一个用于从手机中检索所有播放列表,然后,对于每个播放列表,我获取其中的歌曲并将其添加到播放列表对象。为此,我在所有歌曲列表中搜索与播放列表中的 ID 匹配的 ID。问题是 ID 不匹配。我肯定知道前任。一个播放列表上有 3 首歌曲,但它找到的 ID 与其中一首歌曲完全不同。这是我用来检索所有歌曲的方法:

public ArrayList<Song> getSongList(){
ArrayList<Song> songs = new ArrayList<Song>();
//query external audio
ContentResolver musicResolver = getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
//iterate over results if valid
if(musicCursor!=null && musicCursor.moveToFirst()){
//get columns
int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
int albumColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ALBUM);
int durationColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DURATION);
int dataColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DATA);
int displayNameColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.DISPLAY_NAME);
//add songs to list
do {
String name;
if(musicCursor.getString(titleColumn)==null || musicCursor.getString(titleColumn).isEmpty()) name=musicCursor.getString(displayNameColumn);
else name=musicCursor.getString(titleColumn);

songs.add(new Song(musicCursor.getLong(idColumn), name, musicCursor.getString(albumColumn), musicCursor.getString(artistColumn), musicCursor.getString(durationColumn), musicCursor.getString(dataColumn)));


}
while (musicCursor.moveToNext());
}

这是播放列表:

public void getPlayists() {
playlists = new ArrayList<Playlist>();
// query external audio
String[] projection = {
android.provider.MediaStore.Audio.Playlists._ID,
android.provider.MediaStore.Audio.Playlists.NAME };

String[] projectionPl = {
android.provider.MediaStore.Audio.Media._ID,
android.provider.MediaStore.Audio.Media.TITLE };

ContentResolver resolver = context.getContentResolver();

Uri playlistsUri = android.provider.MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
Cursor playlistsCursor = resolver.query(playlistsUri, projection, null,
null, null);
// iterate over results if valid
if (playlistsCursor != null && playlistsCursor.moveToFirst()) {
// get columns
int idColumn = playlistsCursor
.getColumnIndex(android.provider.MediaStore.Audio.Playlists._ID);
int nameColumn = playlistsCursor
.getColumnIndex(android.provider.MediaStore.Audio.Playlists.NAME);
// add songs to list

do {
long id = playlistsCursor.getLong(idColumn);

Playlist pl = new Playlist(
playlistsCursor.getString(nameColumn), id);

Cursor plCursor = resolver.query(
android.provider.MediaStore.Audio.Playlists.Members
.getContentUri("external", id), projectionPl,
null, null, null);

if (plCursor != null && plCursor.moveToFirst()) {
// get columns
int idColumna = plCursor
.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
int nameColumna = plCursor
.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
// add songs to list
do {
long ida = plCursor.getLong(idColumna);

String title = plCursor.getString(nameColumna);

for (Song song : songs) {

if (song.id == ida) {
pl.addSong(song);
}
}

} while (plCursor.moveToNext());
}
playlists.add(pl);

} while (playlistsCursor.moveToNext());
}

谁能准确解释歌曲 ID 的工作原理,或者我如何修改我的方法才能使它们工作?谢谢。

最佳答案

您的 playlistsCursor 反对 android.provider.MediaStore.Audio.Playlists.Members 而您随后继续从 playlistsCursor 获取值但反对 android.provider.MediaStore.Audio.Media播放列表的返回结构是:

        values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER, playorder);
values.put(MediaStore.Audio.Playlists.Members.AUDIO_ID, audio_id);

因此,如果您正在寻找音频 Id,则更改

    int idColumn = playlistsCursor
.getColumnIndex(android.provider.MediaStore.Audio.Playlists._ID);

    int idColumn = playlistsCursor
.getColumnIndex(MediaStore.Audio.Playlists.Members.AUDIO_ID);

ps:我之前发布过创建播放列表等的代码。在 Google Play 或 Amazon App Store 上查看我的应用播放列表管理器以了解其他功能

关于从播放列表中检索时的 Android 歌曲 ID 与我从媒体中检索时的不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24596948/

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