gpt4 book ai didi

android - 如何使用字符串在 Mediastore 游标中实际搜索歌曲

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

我已经设置了光标,但我仍在为实际搜索它而苦苦挣扎。如何搜索轨道名称?我如何获得它的 ID 以便我可以将它传递给 mediaplayer?我尝试过不同的方法,但没有成功。

基本上,我想知道如何在 mediastore 中搜索某个字符串(在本例中为字符串 songToPlay),获取结果,获取最佳匹配的 ID 并将其传递给我的 mediaplayer 服务以在后台播放.不过,我唯一要问的是如何获得 ID。

这是我使用的代码:

Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] songToPlay = value.split("play song");
String[] searchWords = songToPlay[1].split(" ");
String [] keywords = null;
StringBuilder where = new StringBuilder();
where.append(MediaStore.Audio.Media.TITLE + " != ''");
String selection = where.toString();
String[] projection = {
BaseColumns._ID,
MediaStore.Audio.Media.MIME_TYPE,
MediaStore.Audio.Artists.ARTIST,
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Media.TITLE
};
for (int i = 0; i < searchWords.length; i++) {
keywords[i] = '%' + MediaStore.Audio.keyFor(searchWords[i]) + '%';
}

Cursor cursor = this.managedQuery(uri, projection, selection, keywords, MediaStore.Audio.Media.TITLE);
if (cursor.moveToFirst()) {
do{
test.setText(cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
}while(cursor.moveToNext());
}

我很困惑,大部分代码来自互联网。我已经尝试这样做 2 天了,这就是我在这里问的原因。我自己做了几个小时的尝试。

编辑:当前代码。我应该如何找到位置 - 音乐 ID 或从光标播放该歌曲所需的任何内容?

Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String songToPlay = value.replaceAll("play song", "").trim();
int music_column_index;
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Artists.ARTIST,
MediaStore.Audio.Albums.ALBUM,
MediaStore.Audio.Media.TITLE
};


@SuppressWarnings("deprecation")
Cursor cursor = this.managedQuery(uri,
null,
MediaStore.Audio.Media.TITLE + "=?",
new String[]{songToPlay},
MediaStore.Audio.Media.TITLE + " ASC");

最佳答案

这将在 MediaStore 中搜索所有标题等于您的 songToPlay 字符串值的歌曲。

Cursor cursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null,
MediaStore.Audio.Media.TITLE + "=?",
new String[]{songToPlay},
MediaStore.Audio.Media.TITLE + " ASC");

然后您当然可以查询游标以获取任何结果的 ID(和任何其他列)。

while (cursor.moveToNext()) {
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
// Whatever else you need
}

然而,这将需要精确匹配,您可能希望使用 LIKE 运算符进行更模糊的匹配。

关于android - 如何使用字符串在 Mediastore 游标中实际搜索歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17965034/

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