如何在 exoplayer 中播放本地媒体文件的硬编码内容 uri
how to play hardcoded content uri of local media file in exoplayer
最初我选择了一个 Intent 选择器来选择一个媒体文件,在 onActivityResult
我得到了 uri
fun openVideo(view: View) {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI)
intent.type = "video/*"
startActivityForResult(
Intent.createChooser(intent, "Select video"),
REQUEST_TAKE_GALLERY_VIDEO
)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
...
...
val selectedVideoUri: Uri? = data!!.data
val vidPath: String = selectedVideoUri.toString()
video_uri = Uri.parse(vidPath)
// the video uri looks like below
//content://com.mi.android.globalFileexplorer.myprovider/external_files/Download/Dolittle%20(2020)%20%5B720p%5D%20%5BBluRay%5D%20%5BYTS.MX%5D/Dolittle.2020.720p.BluRay.x264.AAC-%5BYTS.MX%5D.mp4
initializePlayer(video_uri)
...
...
}
}
然后就可以在exoplayer中播放本地文件了
fun initializePlayer(video_uri) {
println("log initializePlayer called")
player = ExoPlayerFactory.newSimpleInstance(this) // `player` was defined in GlobalVariables
id_player_view.player = player
if (video_uri != null) {
println("log $video_uri ${video_uri!!::class.simpleName}")
id_player_view.useController = true
var mediaSource: MediaSource = buildMediaSource(video_uri!!)
player?.playWhenReady = playWhenReady!!
player?.seekTo(currentWindow!!, playBackPosition!!)
player?.prepare(mediaSource, false, false)
}
}
但是现在,想想我把 uri
作为字符串保存在数据库中。
然后,假设我从数据库中检索了内容并将其作为意图传递给视频播放 activity
现在,如果我编写这样的代码,并调用 initializePlayer
视频将无法播放。
video_uri = Uri.parse((intent.getStringExtra("video_url")))
println("log $video_uri ${video_uri!!::class.simpleName}")
initializePlayer(video_uri)
即所有硬编码的 content uri
本地媒体文件。 exoplayer 不工作。
如何解决这个问题?
think that I saved the uri in database as a string
您不再有权访问该内容。
How to solve this issue ?
使用ACTION_OPEN_DOCUMENT
让用户选择内容。然后,在 onActivityResult()
中,在 ContentResolver
上调用 takePersistableUriPermission()
,以便在更长的时间内访问该内容。有关更多信息,请参阅 this blog post and the documentation。
最初我选择了一个 Intent 选择器来选择一个媒体文件,在 onActivityResult
我得到了 uri
fun openVideo(view: View) {
val intent = Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI)
intent.type = "video/*"
startActivityForResult(
Intent.createChooser(intent, "Select video"),
REQUEST_TAKE_GALLERY_VIDEO
)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
...
...
val selectedVideoUri: Uri? = data!!.data
val vidPath: String = selectedVideoUri.toString()
video_uri = Uri.parse(vidPath)
// the video uri looks like below
//content://com.mi.android.globalFileexplorer.myprovider/external_files/Download/Dolittle%20(2020)%20%5B720p%5D%20%5BBluRay%5D%20%5BYTS.MX%5D/Dolittle.2020.720p.BluRay.x264.AAC-%5BYTS.MX%5D.mp4
initializePlayer(video_uri)
...
...
}
}
然后就可以在exoplayer中播放本地文件了
fun initializePlayer(video_uri) {
println("log initializePlayer called")
player = ExoPlayerFactory.newSimpleInstance(this) // `player` was defined in GlobalVariables
id_player_view.player = player
if (video_uri != null) {
println("log $video_uri ${video_uri!!::class.simpleName}")
id_player_view.useController = true
var mediaSource: MediaSource = buildMediaSource(video_uri!!)
player?.playWhenReady = playWhenReady!!
player?.seekTo(currentWindow!!, playBackPosition!!)
player?.prepare(mediaSource, false, false)
}
}
但是现在,想想我把 uri
作为字符串保存在数据库中。
然后,假设我从数据库中检索了内容并将其作为意图传递给视频播放 activity
现在,如果我编写这样的代码,并调用 initializePlayer
视频将无法播放。
video_uri = Uri.parse((intent.getStringExtra("video_url")))
println("log $video_uri ${video_uri!!::class.simpleName}")
initializePlayer(video_uri)
即所有硬编码的 content uri
本地媒体文件。 exoplayer 不工作。
如何解决这个问题?
think that I saved the uri in database as a string
您不再有权访问该内容。
How to solve this issue ?
使用ACTION_OPEN_DOCUMENT
让用户选择内容。然后,在 onActivityResult()
中,在 ContentResolver
上调用 takePersistableUriPermission()
,以便在更长的时间内访问该内容。有关更多信息,请参阅 this blog post and the documentation。