Realize the function of pausing and playing the video and dragging the progress bar in unity

#Unity implements the function of pausing video playback and dragging the progress bar

Realize the function of pausing and playing the video and dragging the progress bar in unity

On the UI, the video contains a play, pause, and stop button, as well as a drag bar. You can use these buttons to control the playback of the video, and use the dragging progress bar to adjust the playback progress of the video.

1. Build a UI, import video material, and then drag and drop the video into the scene.

2. Create a Canvas object as the UI container, then create a new object under the Canvas, and assign the VideoPlayer component to it.

3. Create three buttons on the Canvas: play, pause and stop, and a Slider control for dragging the progress bar.

4. Add UI Click event handlers for the Play, Pause, and Stop buttons respectively, and use the following code snippets to implement the functionality of each button:

The code is as follows (example): This code implements a video playback controller VideoController, which contains the following variables and functions:
Variables:

  1. VideoPlayer videoPlayer: used to control video playback;
  2. Button playButton, pauseButton, stopButton: represent play, pause, and stop buttons respectively;
  3. Slider slider: Used as a progress bar.
    function:
  4. Start(): Add listeners for playButton, pauseButton, stopButton when the script starts;
  5. Update(): Update the value of the slider in each frame as the ratio of the current playback time to the total playback time;
  6. Play(): start playing the video;
  7. Pause(): Pause video playback;
  8. Stop(): stop video playback;
  9. OnSliderValueChanged(): When the value of the slider changes, the video playback time is adjusted to the current value of the slider multiplied by the total playback time.
    This controller can be used in the video player in Unity, allowing users to control the playback of videos through buttons or progress bars on the interface.
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
 public class VideoController : MonoBehaviour
{
    
    
    public VideoPlayer videoPlayer;
    public Button playButton, pauseButton, stopButton;
    public Slider slider;
     void Start()
    {
    
    
        playButton.onClick.AddListener(Play);
        pauseButton.onClick.AddListener(Pause);
        stopButton.onClick.AddListener(Stop);
    }
     void Update()
    {
    
    
        slider.value = (float)videoPlayer.time / (float)videoPlayer.clip.length;
    }
     void Play()
    {
    
    
        videoPlayer.Play();
    }
     void Pause()
    {
    
    
        videoPlayer.Pause();
    }
     void Stop()
    {
    
    
        videoPlayer.Stop();
    }
     public void OnSliderValueChanged()
    {
    
    
        videoPlayer.time = (long)(slider.value * videoPlayer.clip.length);
    }
}

5. Next, assign the VideoPlayer component to the videoPlayer variable in the created VideoController script, and assign the UI elements to the corresponding variables in the script.

6. Finally, assign the On Value Changed event of the Slider control to the OnSliderValueChanged() method in the VideoController script.


Now you should be able to play, pause, stop the video on the UI, and drag the progress bar using the Slider control.

Guess you like

Origin blog.csdn.net/qq_43238378/article/details/129999950