如何获取播放音频文件的音频路径? [英] How to get audio path to play audio file?

查看:142
本文介绍了如何获取播放音频文件的音频路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is my code that gets list of songs:

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

我想通过将歌曲的路径提供给音乐player()方法来播放这首歌.

I want to play that song by giving its path to music player() method.

推荐答案

您可以像这样获得完整路径:

You can get full path like this:

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

因此,您的功能将是:

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 {
        do {

            String fullPath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
            // ...process entry...
            Log.e("Full Path : ", fullPath);


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


    return songlist;
}

这篇关于如何获取播放音频文件的音频路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆