Kotlin 中的视频视图
Kotlin 的VideoView类用于在 android 应用程序中显示视频文件。此类支持3gp和MP4视频格式。 VideoView 类能够播放来自本地存储、特定 URL 或资源文件的视频文件。该类的缺点是它不保留视频文件的完整状态,如果应用程序进入后台,则意味着当前的播放位置、播放状态或任何类型的字幕轨道都无法恢复。
Kotlin 中 VideoView 类的类层次结构
VideoView 小部件的 XML 属性
XML attribute | Description |
---|---|
android:id | Use to uniquely identify a VideoView |
android:layout_width | To set width of the VideoView |
android:layout_height | To set height of the VideoView |
android:layout_margin | To fix the margin from top, bottom, start and end |
app:layout_constraint | To fix the position in an activity |
例子
此示例演示了从本地存储向 android 活动中添加视频文件所涉及的步骤。活动中还添加了一个媒体控制器来控制视频的播放和暂停位置。
Note: Following steps are performed on Android Studio version 4.0
创建新项目
- 单击文件,然后单击新建 => 新建项目。
- 选择语言为 Kotlin。
- 根据您的需要选择最低 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 小部件并为其添加媒体控制器。
Note: Make sure to create a directory named raw in the resource file of your project and add the video file in that directory using file explorer.
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