目前,我正在使用以下代码来搜索SD卡上的特定音频文件名,例如mymusic1.mp3。
private String[] getAudioPath(String songTitle) {
final Cursor mCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA },
MediaStore.Audio.Media.TITLE+ "=?",
new String[] {songTitle},
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
int count = mCursor.getCount();
String[] songs = new String[count];
String[] mAudioPath = new String[count];
int i = 0;
if (mCursor.moveToFirst()) {
do {
songs[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
mAudioPath[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
i++;
} while (mCursor.moveToNext());
}
mCursor.close();
return mAudioPath;
}
但是,我需要将其更改为查找标题为“Let It Be”的歌曲,而不是搜索歌曲文件名以及搜索整个设备,而不仅仅是SD卡。
我尝试调整getContentResolver()。query()来搜索整个设备+搜索歌曲标题元数据,但没有取得多大成功。这可能吗?
谢谢!
答案 0 :(得分:3)
我不确定这是否是正确的做法,但我最终这样做了。我制作了两个游标,然后用MergeCursor将它们包起来,然后使用它。这是代码,如果它有帮助!
private String[] getAudioPath(String songTitle) {
final Cursor mInternalCursor = getContentResolver().query(
MediaStore.Audio.Media.INTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA },
MediaStore.Audio.Media.TITLE+ "=?",
new String[] {songTitle},
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
final Cursor mExternalCursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA },
MediaStore.Audio.Media.TITLE+ "=?",
new String[] {songTitle},
"LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");
Cursor[] cursors = {mInternalCursor, mExternalCursor};
final MergeCursor mMergeCursor = new MergeCursor(cursors);
int count = mMergeCursor.getCount();
String[] songs = new String[count];
String[] mAudioPath = new String[count];
int i = 0;
if (mMergeCursor.moveToFirst()) {
do {
songs[i] = mMergeCursor.getString(mMergeCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
mAudioPath[i] = mMergeCursor.getString(mMergeCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
i++;
} while (mMergeCursor.moveToNext());
}
mMergeCursor.close();
return mAudioPath;
}
答案 1 :(得分:0)
“songTitle”是您要搜索的歌曲名称的字符串。示例:mysong.mp3。