📜  Kotlin中的动态VideoView

📅  最后修改于: 2021-05-13 16:54:45             🧑  作者: Mango

在Android中, VideoView用于加载视频文件。我们可以依靠任何外部资源,URL或本地数据来获取视频内容。在本文中,我们将讨论如何在Kotlin中动态创建VideoView。

视频观看类提供了以下方法,以简化整个过程:

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中创建一个新项目,请按照以下步骤操作:

  1. 单击文件,然后单击新建,然后单击新建项目,并根据需要命名。
  2. 为项目模板选择“空活动”。
  3. 然后,选择Kotlin语言支持,然后单击下一步按钮。
  4. 选择最低的SDK,无论您需要什么

项目目录如下所示:

android-project-directory

修改activity_main.xml文件




新增影片

现在,我们需要添加视频。为此,我们有两个选择:

  1. 我们可以将视频文件本地存储在我们的系统上:
    在res文件夹中创建一个名为“ Raw”的文件夹。在其中添加视频文件,并使用以下代码段。
    // val path = "android.resource://" + packageName + "/" + R.raw.your_videoFile_name
    // videoView.setVideoURI(Uri.parse(path)) 
    
  2. 我们可以使用任何网络资源中的视频文件:
    // 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的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!