简体   繁体   English

奇怪的 URI 行为 java/setDataSource/MediaPlayer

[英]Weird URI behavior java/setDataSource/MediaPlayer

I am using a URI that i pass to a MediaPlayer on android like this:我正在使用我传递给 android 上的 MediaPlayer 的 URI,如下所示:

mediaPlayer.setDataSource(context, Uri.parse(<uri>));

When I get it using Intent.createChooser, it plays once then trying to make it play it again result in java.io.IOException: setDataSource failed.: status=0x80000000.当我使用 Intent.createChooser 获取它时,它播放一次,然后尝试让它再次播放,结果 java.io.IOException: setDataSource failed.: status=0x80000000。

When i pass the URI as a string directly, it results in java.io.IOException: setDataSource failed.: status=0x80000000, despite the URI outputed by the selector being always the same.当我直接将 URI 作为字符串传递时,它会导致 java.io.IOException: setDataSource failed.: status=0x80000000,尽管选择器输出的 URI 始终相同。

The uri look like this: "content://com.android.providers.media.documents/document/audio%3A21739". uri 看起来像这样:“content://com.android.providers.media.documents/document/audio%3A21739”。

Can someone please enlighten on why does this happen?有人可以请说明为什么会发生这种情况吗?

Turns out android required some weird permissions shenanigans, the solutions was using a different opening coupled with using some sort of permission requirement that somehow output the same uri, but with a persistent access:原来 android 需要一些奇怪的权限恶作剧,解决方案是使用不同的开头加上使用某种权限要求,以某种方式输出相同的 uri,但具有持久访问权限:

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                    intent.setType("*/*");
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                    startActivityForResult(Intent.createChooser(intent, "Select file to add"), ADD_2);

In onActivityResult:在 onActivityResult 中:

if (resultCode == RESULT_OK) {
                Uri uri = data.getData();
                final int takeFlags = data.getFlags() & Intent.FLAG_GRANT_READ_URI_PERMISSION;
                getContentResolver().takePersistableUriPermission(uri, takeFlags);
                Log.d(TAG, "Added track uri: " + uri);
                playlist.add(uri.toString());
                adapter.notifyDataSetChanged();
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM