Events.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using UnityEngine.Events;
  2. using System.Collections.Generic;
  3. //-----------------------------------------------------------------------------
  4. // Copyright 2015-2021 RenderHeads Ltd. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6. namespace RenderHeads.Media.AVProVideo
  7. {
  8. [System.Serializable]
  9. public class MediaPlayerLoadEvent : UnityEvent<string> {}
  10. [System.Serializable]
  11. public class MediaPlayerEvent : UnityEvent<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>
  12. {
  13. public enum EventType
  14. {
  15. MetaDataReady, // Triggered when meta data(width, duration etc) is available
  16. ReadyToPlay, // Triggered when the video is loaded and ready to play
  17. Started, // Triggered when the playback starts
  18. FirstFrameReady, // Triggered when the first frame has been rendered
  19. FinishedPlaying, // Triggered when a non-looping video has finished playing
  20. Closing, // Triggered when the media is closed
  21. Error, // Triggered when an error occurs
  22. SubtitleChange, // Triggered when the subtitles change
  23. Stalled, // Triggered when media is stalled (eg. when lost connection to media stream)
  24. Unstalled, // Triggered when media is resumed form a stalled state (eg. when lost connection is re-established)
  25. ResolutionChanged, // Triggered when the resolution of the video has changed (including the load) Useful for adaptive streams
  26. StartedSeeking, // Triggered when seeking begins
  27. FinishedSeeking, // Triggered when seeking has finished
  28. StartedBuffering, // Triggered when buffering begins
  29. FinishedBuffering, // Triggered when buffering has finished
  30. PropertiesChanged, // Triggered when any properties (eg stereo packing are changed) - this has to be triggered manually
  31. PlaylistItemChanged,// Triggered when the new item is played in the playlist
  32. PlaylistFinished, // Triggered when the playlist reaches the end
  33. TextTracksChanged, // Triggered when the text tracks are added or removed
  34. TextCueChanged = SubtitleChange, // Triggered when the text to display changes
  35. // TODO:
  36. //StartLoop, // Triggered when the video starts and is in loop mode
  37. //EndLoop, // Triggered when the video ends and is in loop mode
  38. //NewFrame // Trigger when a new video frame is available
  39. }
  40. private List<UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode>> _listeners = new List<UnityAction<MediaPlayer, EventType, ErrorCode>>(4);
  41. public bool HasListeners()
  42. {
  43. return (_listeners.Count > 0) || (GetPersistentEventCount() > 0);
  44. }
  45. new public void AddListener(UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode> call)
  46. {
  47. if (!_listeners.Contains(call))
  48. {
  49. _listeners.Add(call);
  50. base.AddListener(call);
  51. }
  52. }
  53. new public void RemoveListener(UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode> call)
  54. {
  55. int index = _listeners.IndexOf(call);
  56. if (index >= 0)
  57. {
  58. _listeners.RemoveAt(index);
  59. base.RemoveListener(call);
  60. }
  61. }
  62. new public void RemoveAllListeners()
  63. {
  64. _listeners.Clear();
  65. base.RemoveAllListeners();
  66. }
  67. }
  68. #if false
  69. public interface IMediaEvents
  70. {
  71. void AddEventListener(UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode> call);
  72. void RemoveListener(UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode> call);
  73. void RemoveAllEventListeners();
  74. }
  75. public partial class BaseMediaPlayer
  76. {
  77. void AddEventListener(UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode> call)
  78. {
  79. }
  80. void RemoveListener(UnityAction<MediaPlayer, MediaPlayerEvent.EventType, ErrorCode> call)
  81. {
  82. }
  83. void RemoveAllEventListeners()
  84. {
  85. }
  86. private MediaPlayerEvent _eventHandler;
  87. }
  88. #endif
  89. }