2

目前,我正在使用以下代码在 sdcard 上搜索特定的音频文件名,例如 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() 以搜索整个设备 + 搜索歌曲标题元数据,但没有太大成功。这可能吗?

谢谢!

4

2 回答 2

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;
    }
于 2013-09-04T07:52:42.093 回答
-1

“songTitle”是您要搜索的歌曲名称的字符串。示例:mysong.mp3。

于 2015-03-05T03:07:55.457 回答