我正在按照我的课程说明创建一个视频播放器应用程序,但我陷入了代码

问题描述 投票:0回答:1

我在 URI 导入部分以及绑定解析中遇到错误。 尝试了AI,但没有给出相关结果,该视频播放器播放res文件夹中的视频。 错误图片

package com.example.video_player

import android.os.Bundle
import android.widget.MediaController
import android.widget.VideoView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        setContentView(R.layout.activity_main)
        // Find the VideoView class by its id
        val videoView = findViewById<VideoView>(binding.testView.id)
        // Creating MediaController
        val mediaController = MediaController(this)
        mediaController.setAnchorView(videoView)
        // specify the location of media file
        val uri: Uri = parse("android.resource://" + packageName + "/raw/test")
        // Setting MediaController and URI, then starting the videoView
        videoView.setMediaController(mediaController)
        videoView.setVideoURI(uri)
        videoView.requestFocus()
        videoView.start()
    }
}
android kotlin android-activity
1个回答
0
投票

您的代码中很少有问题,特别是 Uri 的导入和 findViewById 绑定的使用

将此添加到您的导入中:这是针对 URI 的

import android.net.Uri

您正在使用 binding.testView.id,但您当前的布局中似乎没有使用 ViewBinding 或 DataBinding。

您应该直接使用 findViewById 和正确的资源 ID (R.id.testView)

同时更正您的 URI 解析代码:

val uri: Uri = Uri.parse("android.resource://" + packageName + "/raw/test")

这是代码的更正版本

package com.example.video_player

import android.net.Uri
import android.os.Bundle
import android.widget.MediaController
import android.widget.VideoView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        // Adjust window insets if needed
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        // Find the VideoView by its id
        val videoView = findViewById<VideoView>(R.id.testView)

        // Create and set MediaController for videoView
        val mediaController = MediaController(this)
        mediaController.setAnchorView(videoView)

        // Specify the location of the media file (video in res/raw folder)
        val uri: Uri = Uri.parse("android.resource://" + packageName + "/raw/test")

        // Set the MediaController and URI, then start the videoView
        videoView.setMediaController(mediaController)
        videoView.setVideoURI(uri)
        videoView.requestFocus()
        videoView.start()
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.