Make a video player with the unity plug-in AVProVideo

One: Interface display

The function keys are: play/pause, loop playback, volume control, double speed control, video switching, backward and forward, display time.

Two: unity panel

Drag the corresponding content to the corresponding position.

Three: Code

using RenderHeads.Media.AVProVideo;
using System. Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

public class VideoController : MonoBehaviour
{
  
     //Hold the components that control video playback
     public MediaPlayer mediaPlayer;
 
     //Hold the play pause toggle switch
     public Toggle m_videoToggle;
 
     //Hold whether to loop the switch
     public Toggle m_loopToggle;
 
     //Hold the drop-down list that controls the playback speed
     public Dropdown m_playSpeedDropdown;
 
     //Hold the progress bar that controls the video playback progress
     public Slider m_processSlider;
 
     //Hold to display the time display of current playback and total playback
     public Text m_videoTimeTex;
 
     //Hold the back button a few seconds ago
     public Button m_backSecondsBtn;
     //Set the number of seconds returned by each click
     private float m_backSeconds = 3f;
 
     //Hold the Slider that controls the volume
     public Slider m_volumeSlider;
     //Hold the mute switch
     public Toggle m_muteToggle;
     //Store the volume set by the user before the mute state
     private float m_customVolume;

    //Switch to previous video, next video
    public Button lastBtn;
    public Button nextBtn;

    [Header("Video Name")]
    string[] VideoNames;

    //initialization
    void Awake()
     {
        //path to the video name
        VideoNames = File.ReadAllLines(Application.streamingAssetsPath + "/name.txt");

        #region event listener
        //Register play pause switch event
        m_videoToggle.onValueChanged.AddListener(DoPlayOrPause);
         //Register whether to loop the switch event
         m_loopToggle.onValueChanged.AddListener(DoSetLoop);
         //Register the scrolling day trigger event that controls the speed
        
         //Add dropdown list options
         m_playSpeedDropdown.options.Add(new Dropdown.OptionData( "2x speed"));
         m_playSpeedDropdown.options.Add(new Dropdown.OptionData( "1.5x speed"));
         m_playSpeedDropdown.options.Add(new Dropdown.OptionData( "1x speed"));
         m_playSpeedDropdown.options.Add(new Dropdown.OptionData( "0.5 times"));

         //Set the initial speed display value, the default is 1x speed
         m_playSpeedDropdown. value = 2;
         m_playSpeedDropdown.captionText.text = m_playSpeedDropdown.options[2].text;

         //Register the trigger event of the drop-down list that controls the speed
         m_playSpeedDropdown.onValueChanged.AddListener(DoChangeSpeed);
 
         //Register video playback progress bar value change trigger event
         m_processSlider.onValueChanged.AddListener(OnProcessSliderChange);
 
         //Registration returns n seconds before the button triggers the event
         m_backSecondsBtn.onClick.AddListener(OnBackSecondsClick);
 
         //Register volume Slider event
         m_volumeSlider.onValueChanged.AddListener(OnVolumeSliderChange);
         //Register mute switch event
         m_muteToggle.onValueChanged.AddListener(OnMuteToggleClick);
 
         //Register video playback trigger event
         mediaPlayer.Events.AddListener(MediaEventHandler);

        //previous button listener
        lastBtn.onClick.AddListener(LastVideo);
        // next button listener
        nextBtn.onClick.AddListener(NextVideo);
        #endregion
    }

    // Video playback time trigger
    private void MediaEventHandler(MediaPlayer arg0, MediaPlayerEvent. EventType arg1, ErrorCode arg2)
     {
         switch (arg1)
         {
             case MediaPlayerEvent.EventType.Closing:
                 Debug.Log("Close the player to trigger");
                 break;
             case MediaPlayerEvent.EventType.Error:
                 Debug.Log("Triggered when an error is reported");
                 break;
             case MediaPlayerEvent .EventType .FinishedPlaying ://Note: This is not triggered if the video is set to loop playback mode
                 Debug.Log("Play complete trigger");
                 break;
             case MediaPlayerEvent.EventType.FirstFrameReady:
                 Debug.Log("Trigger after preparation");
                 break;
             case MediaPlayerEvent.EventType.MetaDataReady:
                 Debug.Log("triggered during media data preparation");
                 break;
             case MediaPlayerEvent.EventType.ReadyToPlay:
                 Debug.Log("Ready to play trigger");
                 break;
             case MediaPlayerEvent .EventType .Started ://Note: It will be triggered once after each pause
                 Debug.Log("start playing trigger");
                 break;
             default:
                 break;
         }
     }
 
     void Start()
     {
         LoadVideo();
 
         //Initialization trigger once (synchronous sound size)
         OnVolumeSliderChange(m_volumeSlider.value);
     }
 
     void Update()
     {
         //Time to update the playback progress method
         DoUpdateVideoProcess();
 
         //Update the playback time display method at all times
         UpdateTimeText();
     }

    // load video method
    int index = 0;
    void LoadVideo()
     {
         //Load by the method in the plug-in (parameters: 1. Loading path format (corresponding to the panel) 2. Loaded file name 3. Whether to start playing by default)
         mediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder, VideoNames[index] + ".mp4", true);
     }

    #region video toggle

    // next video
    void NextVideo()
    {
        index + + ;
        if (index > VideoNames. Length - 1)
            index = 0;
        //Video playback
        LoadVideo();
    }

    // previous video
    void LastVideo()
    {
        index--;
        if (index<0)
            index = VideoNames. Length - 1;
        //Video playback
        LoadVideo();
    }

    #endregion

    #region video play, pause, loop
    // Play and pause switch click trigger
    void DoPlayOrPause(bool s_isOn)
     {
         //If playing, start playing and text display "play"
         if (s_isOn)
         {
             //Manipulate through the held MediaPlayer class
             mediaPlayer. Control. Play();
             //Change the text displayed by the play switch
             m_videoToggle.transform.Find("VideoText").GetComponent<Text>().text = "Pause";
         }
             // Otherwise, pause
         else
         {
             mediaPlayer. Control. Pause();
             //Change the text displayed by the play switch
             m_videoToggle.transform.Find("VideoText").GetComponent<Text>().text = "Play";
         }
     }

     // Whether to loop the switch click trigger
     void DoSetLoop(bool s_isOn)
     {
         //Manipulate through the held MediaPlayer class
         mediaPlayer.Control.SetLooping(s_isOn);
     }
    #endregion

    #region double speed
    // Change the playback speed method (positive accelerated projection, negative reverse projection)
    void DoChangeSpeed(int s_speed)//The corresponding index value of the drop-down list
     {
         float tSpeed = 1;
         switch (s_speed)
         {
             case 0:
                 tSpeed = 2;
                 break;
             case 1:
                 tSpeed = 1.5f;
                 break;
             case 2:
                 tSpeed = 1;
                 break;
             case 3:
                 tSpeed = 0.5f;
                 break;
             
             default:
                 Debug. Assert(false);
                 break;
         }
         mediaPlayer.Control.SetPlaybackRate(tSpeed);
     }
    #endregion

    #region progress bar
    // Update the video progress to the slider at all times
    void DoUpdateVideoProcess()
     {
         // Get the current playing time
         float tCurrentTime = mediaPlayer.Control.GetCurrentTimeMs();
         // Get the total length of the video
         float tVideoTime = mediaPlayer.Info.GetDurationMs();
         //Calculate the corresponding playback progress and assign it to the progress bar that displays the playback progress
         m_processSlider.value = tCurrentTime/tVideoTime;
     }
 
     // Update the time display of the playback progress
     void UpdateTimeText()
     {
         //Convert the time format for the current playback time
         // convert to seconds
         int tCurrentSeconds = (int)mediaPlayer.Control.GetCurrentTimeMs()/1000;
         // get the current score
         int tCurrentMin = tCurrentSeconds/60;
         //Reassign how many seconds left
         tCurrentSeconds = tCurrentSeconds`;
         string tCurrentSecondStr = tCurrentSeconds < 10 ? "0" + tCurrentSeconds : tCurrentSeconds.ToString();
 
         //Convert the time format for the total time
         // convert to seconds
         int tVideoTimeSeconds = (int) mediaPlayer.Info.GetDurationMs()/1000;
         // get the total score
         int tVideoTimeMin = tVideoTimeSeconds/60;
         // Re-copy how many seconds are left
         tVideoTimeSeconds = tVideoTimeSeconds`;
         string tVideoTimeSecondStr = tVideoTimeSeconds < 10 ? "0" + tVideoTimeSeconds : tVideoTimeSeconds.ToString();
 
         // Splice the time display string
         string tTime = string.Format("<color=black>{0}:{1}/{2}:{3}</color>", tCurrentMin, tCurrentSecondStr, tVideoTimeMin, tVideoTimeSecondStr);
 
         m_videoTimeTex.text = tTime;;
     }
 
     // Triggered when the value of the video playback progress bar changes
     void OnProcessSliderChange(float value)
     {
         // Get the total length of the video
         float tVideoTime = mediaPlayer.Info.GetDurationMs();
         //Time of the current video
         float tCurrentTime = m_processSlider.value*tVideoTime;
         //Adjust the video time to the corresponding node
         mediaPlayer.Control.Seek(tCurrentTime);

        m_processSlider.transform.Find("Fill Area").Find("Fill").GetComponent<RectTransform>().anchoredPosition = new Vector2(0, 0);
        m_processSlider.transform.Find("Handle Slide Area").Find("Handle").GetComponent<RectTransform>().anchoredPosition = new Vector2(0, 0);
     }
 
 
     // The playback progress bar starts to drag and trigger (triggered by EventTrigger)
     public void OnProcessSliderDragBegin()
     {
         //Pause playback
         mediaPlayer. Control. Pause();
     }
 
     // Trigger when the playback progress bar ends dragging (triggered by EventTrigger)
     public void OnProcessSliderDragEnd()
     {
         //Start playing
         mediaPlayer. Control. Play();
     }
 
     // Return to the previous method a few seconds ago
     void OnBackSecondsClick()
     {
         //Get the current playback progress time
         float tCurrentTime = mediaPlayer.Control.GetCurrentTimeMs();
 
         //Progress time before 10s (if there is the first ten seconds, it will retreat, if it does not exist, it will still be the current time progress)
         tCurrentTime = (tCurrentTime - m_backSeconds * 1000) >= 0 ? tCurrentTime - m_backSeconds*1000 : tCurrentTime;
 
         //Set progress time
         mediaPlayer.Control.Seek(tCurrentTime);
     }
    #endregion

    #region Volume Settings

    // volume progress bar change trigger
    void OnVolumeSliderChange(float value)
     {
         //Save the currently set volume
         if (value != 0)
         {
             m_customVolume = m_volumeSlider. value;
         }
         //Set the volume
         mediaPlayer.Control.SetVolume(value);
         //If the volume is manually adjusted to zero, turn on silent mode
         if (value > 0)
         {
             m_muteToggle.isOn = false;
         }
         else
         {
             m_muteToggle.isOn = true;
         }
     }
 
     // mute switch trigger
     void OnMuteToggleClick(bool s_isOn)
     {
         //if muted
         if (s_isOn)
         {
             //set mute
             m_volumeSlider. value = 0;
             mediaPlayer.Control.SetVolume(0);
         }
             // not mute
         else
         {
             m_volumeSlider.value = m_customVolume;
             mediaPlayer.Control.SetVolume(m_customVolume);
         }
     }
    #endregion

}

The video is stored in the streamingAssets folder, and the video name is stored in a txt file named “name”. By reading the name of the video in name, the playback of the previous and next videos is controlled.

Four: Package

Link: https://pan.baidu.com/s/1A3L3lMaJFHrK2lIGD13NWA?pwd=jl98

Extraction code: jl98