Kotlin:Exoplayer 没有播放视频

Kotlin: Exoplayer is not playing the video

我正在学习 Kotlin 和 Exoplayer,我正在制作一个应用程序,允许用户 select 视频并在下一个屏幕上播放。但是,启动 Intent 后,selected 视频没有播放。

第一个Activity:

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)

            }

        }

第二个Activity:

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)

如何解决这个错误?提前致谢。

错误很简单... 只需申请 READ_EXTERNAL_STORAGE 和 WRITE_EXTERNAL_STORAGE 权限

这可以通过 Dexter library 轻松解决。 也许这可以帮助某人