简体   繁体   中英

Weird URI behavior java/setDataSource/MediaPlayer

I am using a URI that i pass to a MediaPlayer on android like this:

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.

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.

The uri look like this: "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:

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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