123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- using RenderHeads.Media.AVProVideo;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class AVProVideoPlayer : MonoBehaviour
- {
- private MediaPlayer mediaPlayer;
- private bool isVideoReady;
- private void Start()
- {
- mediaPlayer = GetComponent<MediaPlayer>();
- mediaPlayer.Events.AddListener(OnVideoReady);
-
- }
-
- /// <summary>
- /// 当前视频状态
- /// </summary>
- /// <param name="mp"></param>
- /// <param name="et"></param>
- /// <param name="errorCode"></param>
- private void OnVideoReady(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
- {
- switch (et)
- {
- case MediaPlayerEvent.EventType.MetaDataReady:
- isVideoReady = true;
- break;
- case MediaPlayerEvent.EventType.ReadyToPlay:
- isVideoReady = true;
- break;
- case MediaPlayerEvent.EventType.Started:
- break;
- case MediaPlayerEvent.EventType.FirstFrameReady:
- break;
- case MediaPlayerEvent.EventType.FinishedPlaying:
- break;
- case MediaPlayerEvent.EventType.Closing:
- break;
- case MediaPlayerEvent.EventType.Error:
- break;
- case MediaPlayerEvent.EventType.SubtitleChange:
- break;
- case MediaPlayerEvent.EventType.Stalled:
- break;
- case MediaPlayerEvent.EventType.Unstalled:
- break;
- case MediaPlayerEvent.EventType.ResolutionChanged:
- break;
- case MediaPlayerEvent.EventType.StartedSeeking:
- break;
- case MediaPlayerEvent.EventType.FinishedSeeking:
- break;
- case MediaPlayerEvent.EventType.StartedBuffering:
- break;
- case MediaPlayerEvent.EventType.FinishedBuffering:
- break;
- case MediaPlayerEvent.EventType.PropertiesChanged:
- break;
- case MediaPlayerEvent.EventType.PlaylistItemChanged:
- break;
- case MediaPlayerEvent.EventType.PlaylistFinished:
- break;
- case MediaPlayerEvent.EventType.TextTracksChanged:
- break;
- default:
- break;
- }
- switch (errorCode)
- {
- case ErrorCode.None:
- break;
- case ErrorCode.LoadFailed: // 加载失败
- break;
- case ErrorCode.DecodeFailed: // 解码失败
- break;
- default:
- break;
- }
- }
- private MediaPlayer MediaPlayer
- {
- get
- {
- if(mediaPlayer == null)
- {
- mediaPlayer = GetComponent<MediaPlayer>();
- }
- return mediaPlayer;
- }
- }
- public void Play()
- {
- MediaPlayer.Play();
-
- }
- public void Stop()
- {
- MediaPlayer.Stop();
- }
- public void Pause()
- {
- MediaPlayer.Pause();
- }
- public void SetUrl(string url)
- {
- isVideoReady = false;
- MediaPlayer.MediaPath = new MediaPath(url, MediaPathType.AbsolutePathOrURL);
- }
- public string GetUrl()
- {
- return MediaPlayer.MediaPath.Path;
- }
- public void SetVolue( float volume)
- {
- MediaPlayer.AudioVolume = volume;
- }
- public void SetSeek(float value)
- {
- long targetTimeMs = (long)(value * 1000f);
- MediaPlayer.Control.Seek(targetTimeMs);
- }
- public float GetMaxTimer()
- {
- return (float)MediaPlayer.Info.GetDuration();
- }
- public float GetNowTimer()
- {
- return (float)MediaPlayer.Control.GetCurrentTime();
- }
- public bool IsVideoReady()
- {
- return isVideoReady;
- }
-
- public float GetVolue()
- {
- return MediaPlayer.AudioVolume;
- }
- public bool IsPlaying()
- {
- if(MediaPlayer.Control == null||MediaPlayer.MediaPath.Path==null)
- return false;
- try
- {
- return MediaPlayer.Control.IsPlaying();
- }
- catch (System.Exception e)
- {
- Debug.LogError(e.Message);
- return false;
-
- }
-
- }
- public float GetVideoWidth()
- {
- return MediaPlayer.Info.GetVideoWidth();
- }
- public float GetVideoHeight()
- {
- return mediaPlayer.Info.GetVideoHeight();
- }
- public bool IsPaused()
- {
- return MediaPlayer.Control.IsPaused();
- }
- public bool IsFinished()
- {
- return MediaPlayer.Control.IsFinished();
- }
-
-
- }
|