簡體   English   中英

Kotlin:Exoplayer沒有播放視頻

[英]Kotlin: Exoplayer is not playing the video

我正在學習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)

如何解決這個錯誤? 提前致謝。

錯誤很簡單...僅請求READ_EXTERNAL_STORAGE和WRITE_EXTERNAL_STORAGE權限

這可以通過Dexter庫輕松解決。 也許這可以幫助某人

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM