[Android] Simple Play Video

Playing Video

Using kotlin

Using MediaController+VideoView to Realize Video Playing and Common Control
This is the simplest way to implement Android, which is provided by the native Android framework. It has better compatibility.

VideoView inherits SurfaceView and implements the MediaPlayerControl interface, which can be placed directly in the layout.
MediaController is an Android-encapsulated auxiliary controller with pause, playback, stop, progress bar and other controls, which can be associated with VideoView before playback.

Introduction to demo
demo Overall Introduction:
A MainActivity is responsible for the permission application, and a fragment is placed in the layout where navigation is responsible for page jumps.
Two fragments, Video fragment for video playback, Video List fragment for video listing

Key code and parsing

Here's how to implement video playback and control by key point code. Only key code is shown.
Layout file fragment_video.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        ... />

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="list"
        .../>


    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btMode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Single tune circulation"  
        .../>


    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btReplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="To replay"
        .../>

</androidx.constraintlayout.widget.ConstraintLayout>

There are two global values in demo. One is to indicate the video index and the other is to list all the videos. In a project, it can be encapsulated in viewModle

/** Index of currently playing video */
var curIndexOfVideos = 0

/** Video Address List */
val videoPathList = listOf(
    PathUtils.getExternalStoragePath() + File.separator + "routon" + File.separator +
            "3.avi",
    PathUtils.getExternalStoragePath() + File.separator + "routon" + File.separator +
            "1.mp4",
    PathUtils.getExternalStoragePath() + File.separator + "routon" + File.separator +
            "55747818-1-192.mkv"
)

Connect VideoView with Media Controller to play video

 /** Setting Video View Video Address */
 fun setVideoPath() {
     videoView.setVideoPath(videoPathList[curIndexOfVideos])
 }

    
 override fun onActivityCreated(savedInstanceState: Bundle?) {
      ....
      setVideoPath()
     //Create a MediaController object
     val mediaController = MediaController(activity)
     //Connecting VideoView with Media Controller
     videoView.setMediaController(mediaController)
     //Let VideoView get the focus
     videoView.requestFocus()
     //Start playing
     videoView.start()
     ...
}
    

Playback Control

Next song

/** Play the next song */
fun palyNextView() {
    curIndexOfVideos++
    if (curIndexOfVideos == videoPathList.size) {
        curIndexOfVideos = 0
    }
    setVideoPath()
    videoView.start()
}


 /** Play the last song */
fun playPrevVideo() {
    curIndexOfVideos--
    if (curIndexOfVideos < 0) {
        curIndexOfVideos = videoPathList.size - 1
    }
    setVideoPath()
    videoView.start()
}
 //Registered Monitoring
mediaController.setPrevNextListeners({
            //Click on the next song
            LogUtils.i("next")
            palyNextView()
        }, {
             //Click on the previous song
            LogUtils.i("prev")
            playPrevVideo()
        })

Pause/play is implemented by MediaController

Cyclic mode

//Controlled by a flag bit
var singleCycle = false
//Click bt to control the flag
btMode.apply {
    text = if (singleCycle) {
        "Single tune circulation"
    } else {
        "List loops"
    }
    clickWithTrigger {
        singleCycle = !singleCycle
        text = if (singleCycle) {
            "Single tune circulation"
        } else {
            "List loops"
        }
    }
}
 //Monitor after registration and playback is completed
videoView.setOnCompletionListener {
    if (singleCycle) {
        //Locate at the start position and start playing.
        videoView.seekTo(0)
        videoView.start()
    } else {
         //Play the next song
        palyNextView()
    }
}

To replay

btReplay.clickWithTrigger {
    //Call videoView's own method
    videoView.resume()
}

List click-and-play

A simple list. Click to modify the global variable curIndex of Videos and return to the video interface

Last

If you see it here and think it's a good article, give it a compliment! _____________ Welcome to comment and discuss! If you think it's worth improving, please leave me a message. We will make serious inquiries, correct shortcomings, and regularly share technical dry goods free of charge. Thank you!

Tags: Android Fragment xml SurfaceView

Posted on Sat, 24 Aug 2019 09:05:08 -0400 by wtg21.org