AVProVideoPlayer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using RenderHeads.Media.AVProVideo;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class AVProVideoPlayer : MonoBehaviour
  6. {
  7. private MediaPlayer mediaPlayer;
  8. private bool isVideoReady;
  9. private void Start()
  10. {
  11. mediaPlayer = GetComponent<MediaPlayer>();
  12. mediaPlayer.Events.AddListener(OnVideoReady);
  13. }
  14. /// <summary>
  15. /// 当前视频状态
  16. /// </summary>
  17. /// <param name="mp"></param>
  18. /// <param name="et"></param>
  19. /// <param name="errorCode"></param>
  20. private void OnVideoReady(MediaPlayer mp, MediaPlayerEvent.EventType et, ErrorCode errorCode)
  21. {
  22. switch (et)
  23. {
  24. case MediaPlayerEvent.EventType.MetaDataReady:
  25. isVideoReady = true;
  26. break;
  27. case MediaPlayerEvent.EventType.ReadyToPlay:
  28. isVideoReady = true;
  29. break;
  30. case MediaPlayerEvent.EventType.Started:
  31. break;
  32. case MediaPlayerEvent.EventType.FirstFrameReady:
  33. break;
  34. case MediaPlayerEvent.EventType.FinishedPlaying:
  35. break;
  36. case MediaPlayerEvent.EventType.Closing:
  37. break;
  38. case MediaPlayerEvent.EventType.Error:
  39. break;
  40. case MediaPlayerEvent.EventType.SubtitleChange:
  41. break;
  42. case MediaPlayerEvent.EventType.Stalled:
  43. break;
  44. case MediaPlayerEvent.EventType.Unstalled:
  45. break;
  46. case MediaPlayerEvent.EventType.ResolutionChanged:
  47. break;
  48. case MediaPlayerEvent.EventType.StartedSeeking:
  49. break;
  50. case MediaPlayerEvent.EventType.FinishedSeeking:
  51. break;
  52. case MediaPlayerEvent.EventType.StartedBuffering:
  53. break;
  54. case MediaPlayerEvent.EventType.FinishedBuffering:
  55. break;
  56. case MediaPlayerEvent.EventType.PropertiesChanged:
  57. break;
  58. case MediaPlayerEvent.EventType.PlaylistItemChanged:
  59. break;
  60. case MediaPlayerEvent.EventType.PlaylistFinished:
  61. break;
  62. case MediaPlayerEvent.EventType.TextTracksChanged:
  63. break;
  64. default:
  65. break;
  66. }
  67. switch (errorCode)
  68. {
  69. case ErrorCode.None:
  70. break;
  71. case ErrorCode.LoadFailed: // 加载失败
  72. break;
  73. case ErrorCode.DecodeFailed: // 解码失败
  74. break;
  75. default:
  76. break;
  77. }
  78. }
  79. private MediaPlayer MediaPlayer
  80. {
  81. get
  82. {
  83. if(mediaPlayer == null)
  84. {
  85. mediaPlayer = GetComponent<MediaPlayer>();
  86. }
  87. return mediaPlayer;
  88. }
  89. }
  90. public void Play()
  91. {
  92. MediaPlayer.Play();
  93. }
  94. public void Stop()
  95. {
  96. MediaPlayer.Stop();
  97. }
  98. public void Pause()
  99. {
  100. MediaPlayer.Pause();
  101. }
  102. public void SetUrl(string url)
  103. {
  104. isVideoReady = false;
  105. MediaPlayer.MediaPath = new MediaPath(url, MediaPathType.AbsolutePathOrURL);
  106. }
  107. public string GetUrl()
  108. {
  109. return MediaPlayer.MediaPath.Path;
  110. }
  111. public void SetVolue( float volume)
  112. {
  113. MediaPlayer.AudioVolume = volume;
  114. }
  115. public void SetSeek(float value)
  116. {
  117. long targetTimeMs = (long)(value * 1000f);
  118. MediaPlayer.Control.Seek(targetTimeMs);
  119. }
  120. public float GetMaxTimer()
  121. {
  122. return (float)MediaPlayer.Info.GetDuration();
  123. }
  124. public float GetNowTimer()
  125. {
  126. return (float)MediaPlayer.Control.GetCurrentTime();
  127. }
  128. public bool IsVideoReady()
  129. {
  130. return isVideoReady;
  131. }
  132. public float GetVolue()
  133. {
  134. return MediaPlayer.AudioVolume;
  135. }
  136. public bool IsPlaying()
  137. {
  138. if(MediaPlayer.Control == null||MediaPlayer.MediaPath.Path==null)
  139. return false;
  140. try
  141. {
  142. return MediaPlayer.Control.IsPlaying();
  143. }
  144. catch (System.Exception e)
  145. {
  146. Debug.LogError(e.Message);
  147. return false;
  148. }
  149. }
  150. public float GetVideoWidth()
  151. {
  152. return MediaPlayer.Info.GetVideoWidth();
  153. }
  154. public float GetVideoHeight()
  155. {
  156. return mediaPlayer.Info.GetVideoHeight();
  157. }
  158. public bool IsPaused()
  159. {
  160. return MediaPlayer.Control.IsPaused();
  161. }
  162. public bool IsFinished()
  163. {
  164. return MediaPlayer.Control.IsFinished();
  165. }
  166. }