如何获取播放音频文件的音频路径?

时间:2016-03-14 11:02:54

标签: android android-mediaplayer android-cursor android-contentresolver mediastore

这是我获取歌曲列表的代码:

  List<String> getsonglist() {

        List<String> songlist = new ArrayList<>();
        ContentResolver contentResolver = getContentResolver();
        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

        Cursor cursor = contentResolver.query(uri, null, null, null, null);
        if (cursor == null) {
            // query failed, handle error.
        } else if (!cursor.moveToFirst()) {
            // no media on the device
        } else {
            int titleColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
            int idColumn = cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
            do {
                long thisId = cursor.getLong(idColumn);
                String thisTitle = cursor.getString(titleColumn);
                // ...process entry...

                songlist.add(thisTitle);
            } while (cursor.moveToNext());
        }


        return songlist;
    }

我希望通过提供音乐播放器()方法的路径播放该歌曲。

2 个答案:

答案 0 :(得分:2)

你可以得到这样的完整路径:

bin

所以,你的功能是:

String fullPath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));

答案 1 :(得分:0)

试试这个

String fullPath = cursor.getString(cursor.getColumnIndex("_data"));