【发布时间】:2019-08-05 15:28:10
【问题描述】:
我正在学习 Kotlin 和 Exoplayer,我正在制作一个允许用户选择视频并在下一个屏幕上播放的应用程序。但是,启动 Intent 后,选择的视频并没有播放。
第一个活动:
private fun openGallery() {
intent = Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
intent.type = "video/*"
startActivityForResult(intent, 1)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK && requestCode == 1) {
val selectedMediaUri = data!!.getData()
if (selectedMediaUri.toString().contains("video")) {
//handle video
// Get selected gallery image
val selectedVideo = data.getData()
// Get and resize profile image
val filePathColumn = arrayOf(MediaStore.Video.Media.DATA)
val cursor = applicationContext.contentResolver.query(selectedVideo!!, filePathColumn, null, null, null)
cursor!!.moveToFirst()
val columnIndex = cursor.getColumnIndex(filePathColumn[0])
val videoStoragePath = cursor.getString(columnIndex)
cursor.close()
val intent = Intent(this@MainActivity,PostarVideoActivity::class.java)
intent.putExtra("path",videoStoragePath)
startActivity(intent)
}
}
第二个活动:
val path = intent.extras!!.getString("path")
Log.i("mypath", path)
val player = ExoPlayerFactory.newSimpleInstance(applicationContext)
videoPreview.player = player
val dataSourceFactory = DefaultDataSourceFactory(
applicationContext,
Util.getUserAgent(applicationContext, applicationContext.getString(R.string.app_name))
)
// This is the MediaSource representing the media to be played.
val videoSource = ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(path!!))
// Prepare the player with the source.
player.prepare(videoSource)
如何解决这个错误?提前致谢。
【问题讨论】:
-
你应该从查看你的 logcat 开始。如果您无法弄清楚,请使用一些可能有助于理解问题的日志更新您的问题。