📜  Kotlin 中的视频视图

📅  最后修改于: 2022-05-13 01:54:28.674000             🧑  作者: Mango

Kotlin 中的视频视图

Kotlin 的VideoView类用于在 android 应用程序中显示视频文件。此类支持3gpMP4视频格式。 VideoView 类能够播放来自本地存储、特定 URL 或资源文件的视频文件。该类的缺点是它不保留视频文件的完整状态,如果应用程序进入后台,则意味着当前的播放位置、播放状态或任何类型的字幕轨道都无法恢复。

Kotlin 中 VideoView 类的类层次结构

Kotlin中VideoView类的类层次图

VideoView 小部件的 XML 属性

XML attributeDescription
android:idUse to uniquely identify a VideoView
android:layout_widthTo set width of the VideoView
android:layout_heightTo set height of the VideoView
android:layout_marginTo fix the margin from top, bottom, start and end
app:layout_constraintTo fix the position in an activity

例子

此示例演示了从本地存储向 android 活动中添加视频文件所涉及的步骤。活动中还添加了一个媒体控制器来控制视频的播放和暂停位置。

创建新项目

  1. 单击文件,然后单击新建 => 新建项目。
  2. 选择语言为 Kotlin。
  3. 根据您的需要选择最低 SDK。

在 activity_main.xml 文件中添加 VideoView

下面是activity_main.xml文件的代码,用于在活动中添加 TextView 和 VideoView。

xml


 
    
 
    
 


Java
package com.example.videoviewinkotlin
 
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.MediaController
import android.widget.Toast
import android.widget.VideoView
import androidx.appcompat.app.AppCompatActivity
 
 
class MainActivity :  AppCompatActivity() {
 
    // declaring a null variable for VideoView
    var simpleVideoView: VideoView? = null
 
    // declaring a null variable for MediaController
    var mediaControls: MediaController? = null
 
    override fun onCreate(savedInstanceState: Bundle?){
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        // assigning id of VideoView from
        // activity_main.xml layout file
        simpleVideoView = findViewById(R.id.simpleVideoView) as VideoView
 
        if (mediaControls == null) {
            // creating an object of media controller class
            mediaControls = MediaController(this)
 
            // set the anchor view for the video view
            mediaControls!!.setAnchorView(this.simpleVideoView)
        }
 
        // set the media controller for video view
        simpleVideoView!!.setMediaController(mediaControls)
 
        // set the absolute path of the video file which is going to be played
        simpleVideoView!!.setVideoURI(Uri.parse("android.resource://"
                                       + packageName + "/" + R.raw.gfgvideo))
 
        simpleVideoView!!.requestFocus()
 
        // starting the video
        simpleVideoView!!.start()
 
        // display a toast message
        // after the video is completed
        simpleVideoView!!.setOnCompletionListener {
            Toast.makeText(applicationContext, "Video completed",
                Toast.LENGTH_LONG).show()
        }
 
        // display a toast message if any
        // error occurs while playing the video
        simpleVideoView!!.setOnErrorListener { mp, what, extra ->
            Toast.makeText(applicationContext, "An Error Occured " +
                    "While Playing Video !!!", Toast.LENGTH_LONG).show()
            false
        }
    }
}


Java

    VideoView in Kotlin
    Running a video file in an activity


Java


 
    
        
            
                
 
                
            
        
        
    
 


打开 MainActivity.kt 文件

下面是MainActivity.kt文件的代码,用于访问 Kotlin 文件中的 VideoView 小部件并为其添加媒体控制器。

Java

package com.example.videoviewinkotlin
 
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.MediaController
import android.widget.Toast
import android.widget.VideoView
import androidx.appcompat.app.AppCompatActivity
 
 
class MainActivity :  AppCompatActivity() {
 
    // declaring a null variable for VideoView
    var simpleVideoView: VideoView? = null
 
    // declaring a null variable for MediaController
    var mediaControls: MediaController? = null
 
    override fun onCreate(savedInstanceState: Bundle?){
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        // assigning id of VideoView from
        // activity_main.xml layout file
        simpleVideoView = findViewById(R.id.simpleVideoView) as VideoView
 
        if (mediaControls == null) {
            // creating an object of media controller class
            mediaControls = MediaController(this)
 
            // set the anchor view for the video view
            mediaControls!!.setAnchorView(this.simpleVideoView)
        }
 
        // set the media controller for video view
        simpleVideoView!!.setMediaController(mediaControls)
 
        // set the absolute path of the video file which is going to be played
        simpleVideoView!!.setVideoURI(Uri.parse("android.resource://"
                                       + packageName + "/" + R.raw.gfgvideo))
 
        simpleVideoView!!.requestFocus()
 
        // starting the video
        simpleVideoView!!.start()
 
        // display a toast message
        // after the video is completed
        simpleVideoView!!.setOnCompletionListener {
            Toast.makeText(applicationContext, "Video completed",
                Toast.LENGTH_LONG).show()
        }
 
        // display a toast message if any
        // error occurs while playing the video
        simpleVideoView!!.setOnErrorListener { mp, what, extra ->
            Toast.makeText(applicationContext, "An Error Occured " +
                    "While Playing Video !!!", Toast.LENGTH_LONG).show()
            false
        }
    }
}

修改字符串.xml 文件

该文件中列出了活动中使用的所有字符串。

Java


    VideoView in Kotlin
    Running a video file in an activity

打开 AndroidManifest.xml 文件

以下是AndroidManifest.xml文件的代码

Java



 
    
        
            
                
 
                
            
        
        
    
 

作为模拟器运行