Events
The MediaPlayer component generates various events which can be subscribed to.
Events Section
The Events section of the MediaPlayer
component can be usd to assign a method for handling events, however it is usually better to do this via scripting (see below).
Events can also be filtered via the dropdown Triggered Events
list to prevent certain events from triggering which can save some scripting performance.
Scripting
Here is an example of how to add a listener for events and handle those events:
using UnityEngine;
using RenderHeads.Media.AVProVideo;
[RequireComponent(typeof(MediaPlayer))]
public class EventsExample : MonoBehaviour
{
void Awake()
{
// The method HandleEvent will be called whenever an event is triggered
GetComponent<MediaPlayer>().Events.AddListener(HandleEvent);
}
// This method is called whenever there is an event from the MediaPlayer
void HandleEvent(MediaPlayer mp, MediaPlayerEvent.EventType eventType, ErrorCode code)
{
Debug.Log("MediaPlayer " + mp.name + " generated event: " + eventType.ToString());
if (eventType == MediaPlayerEvent.EventType.Error)
{
Debug.LogError("Error: " + code);
}
}
}
Events
Event | Description |
---|---|
MetaDataReady | Triggered when certain data (width, duration etc) is available |
ReadyToPlay | Triggered when the video is loaded and ready to play (not triggered when auto-play is enabled) |
Started | Triggered when the playback starts |
FirstFrameReady | Triggered when the first frame has been rendered (not available on some playforms e.g. WebGL) |
FinishedPlaying | Triggered when a non-looping video has finished playing |
Closing | Triggered when the media is closed |
Error | Triggered when an error occurs |
SubtitleChange | Triggered when the subtitles change |
Stalled | Triggered when media is stalled (e.g. when lost connection to media stream) |
Unstalled | Triggered when media is resumed form a stalled state (e.g. when lost connection is re-established) |
ResolutionChanged | Triggered when the resolution of the video has changed (including the load). Useful for adaptive streams |
StartedSeeking | Triggered when seeking begins |
FinishedSeeking | Triggered when seeking has finished |
StartedBuffering | Triggered when buffering begins |
FinishedBuffering | Triggered when buffering has finished |
PropertiesChanged | Triggered when any properties (e.g. stereo packing are changed) - this has to be triggered manually |
PlaylistItemChanged | Triggered when the new item is played in the playlist |
PlaylistFinished | Triggered when the playlist reaches the end |
TextTracksChanged | Triggered when the text tracks are added or removed |
TextCueChanged | Triggered when the text to display changes |