在Android中, VideoView用于加载视频文件。我们可以依靠任何外部资源,URL或本地数据来获取视频内容。在本文中,我们将讨论如何在Kotlin中动态创建VideoView。
Note: If we use the video view in the background or just go back from a current video session, the old video position is not saved, that is the old state where we last left the video is not saved. In order to achieve it, we need to make use of some external databases to store the states.
视频观看类提供了以下方法,以简化整个过程:
METHOD | DESCRIPTION |
---|---|
public void start() | It is used to start the VideoView |
public void pause() | Pauses the video |
public void resume() | Resumes the video |
public void stopPlayback() | Stops the video |
public void setVideoURI (Uri uri) | It is used to set the path of the Video file which needs to be accesed. It can be a url, or a local path |
public void seekTo(int millis) | Jumps to a specific interval of time |
public void setMediaController(MediaController controller) | This is use to set the controllers of the VideoView (play, pause, fast forward like buttons which you see along the video) |
public void getDuration() | It is used to get the total duration of the video |
public void getCurrentPosition() | It is used to get the current time interval or position of the video |
public void isPlaying() | It return a boolean value in accordance to whether the video is playing or not |
public void setOnPreparedListener(MediaPlayer.OnPreparedListener) | It is a listener which acts when the video is just ready to start |
public void setOnErrorListener(MediaPlayer.OnErrorListener) | It is a listener which acts when an error occurs while playing the video |
public void setOnCompletionListener(MediaPlayer.OnCompletionListener) | It is a listener which acts when the video is completed |
public void setAnchorView(View view) | It sets the position of the controls of media controller on the screen |
在Android Studio中创建一个新项目
要在Android Studuio中创建一个新项目,请按照以下步骤操作:
- 单击文件,然后单击新建,然后单击新建项目,并根据需要命名。
- 为项目模板选择“空活动”。
- 然后,选择Kotlin语言支持,然后单击下一步按钮。
- 选择最低的SDK,无论您需要什么
项目目录如下所示:
修改activity_main.xml文件
新增影片
现在,我们需要添加视频。为此,我们有两个选择:
- 我们可以将视频文件本地存储在我们的系统上:
在res文件夹中创建一个名为“ Raw”的文件夹。在其中添加视频文件,并使用以下代码段。// val path = "android.resource://" + packageName + "/" + R.raw.your_videoFile_name // videoView.setVideoURI(Uri.parse(path))
- 我们可以使用任何网络资源中的视频文件:
// Uri uri = Uri.parse("your_custom_URL"); // videoView.setVideoURI(uri)
在MainActivity.kt文件中创建VideoView
在您的MainActivity.kt
插入以下代码。
package gfg.apps.videoview
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.ViewGroup
import android.widget.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// creating a VideoView
val videoView = VideoView(this)
// setting height and width of the VideoView in our linear layout
val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
layoutParams.setMargins(10, 10, 10, 10)
videoView.layoutParams = layoutParams
// accessing the media controller
val mediaController = MediaController(this)
mediaController.setAnchorView(videoView)
videoView.setMediaController(mediaController)
// setting the video access path
val path = "android.resource://" + packageName + "/" + R.raw.gfg
videoView.setVideoURI(Uri.parse(path))
val linearLayout = findViewById(R.id.layout)
// Add VideoView to LinearLayout
linearLayout?.addView(videoView)
}
}
AndroidManifest.xml文件
在模拟器上运行
想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!