I am trying to play a video which user selects from file explorer. Then it should open and start video as expected and as shown in below tutorial. But there is below error just after it. Can anyone help me out?
Tutorial: https://proandroiddev.com/learn-with-code-jetpack-compose-playing-media-part-3-3792bdfbe1ea
Code:
@Composable
fun VideoView(context: Context, mediaUri: Uri) {
val exoPlayer = remember(context) {
SimpleExoPlayer.Builder(context).build().apply {
val dataSourceFactory: DataSource.Factory = DefaultDataSourceFactory(context,
Util.getUserAgent(context, context.packageName))
val source = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(mediaUri)
this.prepare(source)
this.playWhenReady = true
}
}
DisposableEffect(
AndroidView(
modifier =
Modifier.testTag("VideoPlayer"),
factory = {
PlayerView(context).apply {
player = exoPlayer
layoutParams =
FrameLayout.LayoutParams(
ViewGroup.LayoutParams
.MATCH_PARENT,
ViewGroup.LayoutParams
.WRAP_CONTENT
)
}
}
)
) {
onDispose {
// relase player when no longer needed
exoPlayer.release()
}
}
}
Manifest:
<activity
android:name=".MainActivity"
android:exported="true"
android:label="@string/app_name"
android:theme="@style/Theme.Photoeditorjetpack.NoActionBar">
Error:
022-01-09 16:46:55.247 9796-10228/android.process.media E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri content://com.android.providers.media.documents/document/video:114 from pid=13515, uid=10098 requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:634)
at com.android.providers.media.MediaDocumentsProvider.enforceReadPermissionInner(MediaDocumentsProvider.java:184)
at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:503)
at android.content.ContentProvider$Transport.enforceFilePermission(ContentProvider.java:494)
at android.content.ContentProvider$Transport.openTypedAssetFile(ContentProvider.java:422)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:302)
at android.os.Binder.execTransact(Binder.java:731)
Update: I got to know, we can't pass mediaUri since there are special characters in URI which creates issue with the passing data from one View to another while using Navigation or NavigationHost
Uri
, and what did you do with theUri
between when you got it and when you tried running this code? – BlaineblaineyrememberLauncherForActivityResult
– TressietressureUri
in the same activity instance where you received it? If you passed it to another activity, or you persisted it and loaded it in a later run of your app, that would explain the problem. – BlaineblaineyIntent
are you using withrememberLauncherForActivityResult()
? – BlaineblaineyActivityResultContracts.GetContent()
– TressietressureOpenDocument
and see if you have better luck. That is what the error message suggests (viaACTION_OPEN_DOCUMENT
). – BlaineblaineyGetContent
. – Blaineblaineyjava.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaDocumentsProvider uri content://com.android.providers.media.documents/document/video:114 from pid=6524, uid=10098 requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
– Tressietressure