Unity AVPro video 开始播放,播放完成事件监听

功能:htc 手柄按下touchpad 或者 Grap 播放全景视频 

using RenderHeads.Media.AVProVideo;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Valve.VR;

public class ViveControllerInput : MonoBehaviour
{
    public MediaPlayer _mediaPlayer;
    public SteamVR_Action_Boolean m_TeleportAction; // 圆盘按下事件
    public SteamVR_Action_Boolean m_GrapPinchAction; // Grap 按下事件
    public SteamVR_Action_Single m_GrapPowerAction;  // Grap 按下程度
    public SteamVR_Action_Vector2 m_TeleportMoveAction; // 圆盘触摸事件
    public SteamVR_Behaviour_Pose[] m_pose; // 获取手柄上的 SteamVR_Behaviour_Pose 组件
    bool isPlaying = false;
    private void Start()
    {
        _mediaPlayer.Events.AddListener(OnMediaPlayerEvent);
    }
    void Update()
    {
       
        for (int i = 0; i < m_pose.Length; i++)
        {
            if (m_TeleportAction.GetStateDown(m_pose[i].inputSource)|| m_GrapPinchAction.GetStateDown(m_pose[i].inputSource))
            {
                Debug.Log("任意一个手柄的Teleport圆盘按下 或者是  grap键 按下");
                if (!isPlaying)
                {
                    StartMovie("/AVProVideoSamples/SampleSphere.mp4");
                    Debug.Log("播放或者重新播放");
                }
            }
        }
        
        //// Teleport 圆盘触摸坐标数据获取
        //Vector2 TeleportPos = m_TeleportMoveAction.GetAxis(SteamVR_Input_Sources.Any);
        //Debug.Log("TeleportPos" + TeleportPos);
        //if (TeleportPos != Vector2.zero)
        //{
        //    // Debug.Log("TeleportPos" + TeleportPos);
        //}

        //// Grap 按下程度
        //float m_GrapPower = m_GrapPowerAction.GetAxis(SteamVR_Input_Sources.Any);
        //Debug.Log(m_GrapPower);
    }

    public void OnMediaPlayerEvent(MediaPlayer mp,MediaPlayerEvent.EventType et,ErrorCode errorCode)
    {
        switch (et)
        {
            case MediaPlayerEvent.EventType.Started:
                print("startedEvent开始事件触发");
                OnMediaPlayerStarted(mp);
                break;
            case MediaPlayerEvent.EventType.FinishedPlaying:
                print("finishedEvent结束事件触发");
                OnMediaPlayerFinished(mp);
                break;
        }

    }
    void StartMovie(string mp4Str) {
        _mediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.RelativeToStreamingAssetsFolder, Application.streamingAssetsPath + mp4Str, true);
        _mediaPlayer.m_Loop = false;
        _mediaPlayer.Play();
       
    }
    /// 
    /// 开始事件触发
    /// 
    /// 
    void OnMediaPlayerStarted(MediaPlayer mp)
    {
        print("开始事件触发的后续");
        isPlaying = true;
    }
    /// 
    /// 结束事件触发
    /// 
    /// 
    void OnMediaPlayerFinished(MediaPlayer mp)
    {
        print("结束事件触发的后续");
        isPlaying = false;
    }

}

事件监听的方法转载自:

https://blog.csdn.net/thinkcg/article/details/75093921

你可能感兴趣的:(unity)