AVProVideo 截取其中某一段循环播放

按照国际惯例直接上代码了

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

public class MediaPlayerController : MonoBehaviour
{
    public MediaPlayer PlayingPlayer;
    [SerializeField,Header("是否启用局部播放循环播放")]
    bool isSeeking;
    [SerializeField, Header("StartSeekValue"),Range(0,1)]
    float startSeekValue=0.0f;
    [SerializeField, Header("EndSeekValue"), Range(0, 1)]
    float endSeekValue = 0.0f;
    [SerializeField, Header("StartLoopSeekValue"), Range(0, 1)]
    float startLoopSeekValue = 0.0f;
    [SerializeField,Header("当前播放帧")]
    float currentTime;
    [SerializeField, Header("结束帧")]
    float endSeekTime;
    private void Start()
    {
        if (!PlayingPlayer)
        {
            PlayingPlayer = GetComponent();
        }

        PlayingPlayer.Events.AddListener(OnVideoEvent);
       
    }

    private void OnGUI()
    {
        if (GUILayout.Button("StartSeek"))
        {
            ValuePlay(startSeekValue);
        }
       
    }


    void LateUpdate()
    {
        if (isSeeking)
        {
            currentTime=PlayingPlayer.Control.GetCurrentTimeMs();

            if (currentTime >= endSeekValue * PlayingPlayer.Info.GetDurationMs())
            {
                ValuePlay(startLoopSeekValue);
            }

        }
    }

    public void ValuePlay(float value)
    {
        if (PlayingPlayer)
        {
            PlayingPlayer.Pause();


            if (PlayingPlayer)
            {
                PlayingPlayer.Control.SeekFast(value * PlayingPlayer.Info.GetDurationMs());
            }

            PlayingPlayer.Play();
        }
        
    }


    public void OnVideoEvent(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
    {
        switch (et)
        {
            case MediaPlayerEvent.EventType.ReadyToPlay:
                break;
            case MediaPlayerEvent.EventType.Started:
                break;
            case MediaPlayerEvent.EventType.FirstFrameReady:
                break;
            case MediaPlayerEvent.EventType.FinishedPlaying:
                break;
            case MediaPlayerEvent.EventType.StartedSeeking:
                isSeeking = false;
                endSeekTime=endSeekValue* PlayingPlayer.Info.GetDurationMs();
                break;
            case MediaPlayerEvent.EventType.FinishedSeeking:
                isSeeking = true;
                break;
            default:;break;
        }

        Debug.Log("Event: " + et.ToString());
    }


}
 

你可能感兴趣的:(脚本)