如何使用文件的路径在 Android 中获取 mp3 文件的详细信息,如名称或艺术家?

问题描述

我正在开发 Chrome 操作系统应用程序,我想在将一首歌曲拖放到我的应用程序中的视图上时播放一首歌曲。我创建了一个侦听器,并且能够通过其路径获取拖动的文件。但问题是;我无法获取有关已删除歌曲的信息。

这是我迄今为止尝试过的;

class backingTrackDragListener implements View.OnDragListener {
    private final static Uri ARTWORK_URI = Uri.parse("content://media/external/audio/albumart");
    backingTrackviewmodel viewmodel;

    public backingTrackDragListener(backingTrackviewmodel viewmodel) {
        super();
        this.viewmodel = viewmodel;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public boolean onDrag(View v,DragEvent event) {

        switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                return true;

            case DragEvent.ACTION_DROP:
                if (event.getClipDescription().hasMimeType("application/x-arc-uri-list")) {
                    MainActivity mainActivity = MyApplication.getMainActivity();

                    if (mainActivity == null) {
                        break;
                    }

                    mainActivity.requestDragAndDropPermissions(event);
                    ClipData.Item item = event.getClipData().getItemAt(0);

                    ContentResolver contentResolver = mainActivity.getContentResolver();

                    try {
                        String audioPath = new File(new URI(item.getUri().toString()).getPath()).getCanonicalPath();

                        Cursor cursor = contentResolver.query(
                                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,new String[]{
                                        MediaStore.Audio.Media.TITLE,MediaStore.Audio.Media.DURATION,MediaStore.Audio.Media.ALBUM_ID
                                },MediaStore.Audio.Media.DATA + " = ?",new String[]{audioPath},"");

                        String[] str = cursor.getColumnNames(); // prints the column names

                        final String displayName = cursor.getString(0);
                        final long duration = cursor.getLong(1);
                        final long albumId = cursor.getLong(2);
                        final Uri imageUri = ContentUris.withAppendedId(ARTWORK_URI,albumId);
                        if (audioPath.endsWith("mp3") && duration > 15000) {
                            viewmodel.setSelectedAudio(new backingTrackAudio(displayName,imageUri,audioPath));
                        } else {
                            break;
                        }
                        cursor.close();
                    } catch (IOException | URISyntaxException e) {
                        e.printstacktrace();
                    }

                    break;
                }

            default:
                break;
        }

        return false;
    }
}

这里的实际问题是我无法读取列数据。

我认为问题是在我创建游标时发生的。当我想从游标中获取列数据时,我的应用程序抛出以下错误

Error: android.database.Cursorindexoutofboundsexception: Index -1 requested,with a size of 0

Error line: final String displayName = cursor.getString(0);

我也尝试过使用像 cursor.movetoNext() or cursor.movetoFirst() 这样的 while 循环,但没有成功。

我想要的是从 mp3 文件的路径中获取曲目名称、专辑封面和持续时间。有没有另一种方法来实现这一目标?我也试过这个解决方案:Android: How to get audio detail from audio file 但这个也没有用:/任何建议都会有所帮助,谢谢。

更新

我也试过:

  Cursor cursor = contentResolver.query(
                            item.getUri(),new String[]{
                                    MediaStore.Audio.Media.TITLE,MediaStore.Audio.Media.ALBUM_ID
                            },"");

并且错误与上一个非常相似: android.database.Cursorindexoutofboundsexception: Index -1 requested,with a size of 1

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)