Вопрос

Currently, I am using the following bit of code to search for a specific audio filename on the sdcard, such as 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;
}

However, I need to change this to finding a song with the title "Let It Be" rather than searching for the song filename as well as searching the entire device, not just the SDcard.

I've tried tweaking the getContentResolver().query() to search the entire device + search for the song title metadata, but not having much success. Is this possible?

Thank you!

Это было полезно?

Решение

I'm not sure if this is the correct way of doing it, but I ended up doing it like this. I made two cursors and then wrapped them up with MergeCursor and then used that. Here's the code if it helps!

    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;
    }

Другие советы

"songTitle" is the string of the name of the song you are searching for. Example: mysong.mp3.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top