class in UnityEngine.Video
/
Inherits from:Behaviour
/
Implemented in:UnityEngine.VideoModule
Plays video content onto a target.
Content can be either a VideoClip imported asset or a URL such as file://
or http://
. Video content will be projected onto one of the supported targets, such as camera background or RenderTexture.
If the video content includes transparency, this transparency will be present in the target, allowing objects behind the video target to be visible.
Movie File Format Support Notes
The VideoPlayer uses native audio and video decoding libraries in its implementation and it is your responsibility to use videos that match the requirements for the target platform. The VideoClipImporter offers an option to transcode the VideoClip assets into one of H.264 or VP8 video codecs, along with a few options to experiment with, such as resolution. This uses the matching codec for audio tracks: AAC and Vorbis respectively.
See Also: VideoClipImporter.SetTargetSettings and VideoImporterTargetSettings.enableTranscoding.
You may choose to omit this transcoding and instead use videos that you already know are supported by the target systems, keeping finer control over the encoding process using an external program. Over time, the VideoClipImporter editor will provide guidelines and warnings to better help making proper format and encoding choices.
For now, vendor recommendations must be followed, and are especially constrained on older mobile platforms. For instance, videos you find on the web will often require inspection and manipulations before they can be used reliably in the context of a game running on multiple devices. The following are examples of recommendations and known limitations:
* Android: Supported Media Formats. See additional notes below.
* Windows: H.264 Video Decoder (see Format Constraints)
* iPhone 6-7: Compare iPhone Models (see TV and Video)
The best natively supported video codec for hardware acceleration is H.264, with VP8 being a software decoding solution that can be used when required. On Android, VP8 is also supported using native libraries and as such may also be hardware-assisted depending on models. Key values to look for in your encoding parameters:
* Video Codec: H.264 or VP8.
* Resolution: For example: 1280 x 720.
* Profile: Applicable for H.264. The profile is a set of capabilities and constraints; vendors often specify this, such as "Baseline" or "Main". See here.
* Profile Level: Applicable for H.264. Within a given profile, the level specifies certain performance requirements such as "Baseline 3.1". See here.
* Audio Codec: Typically AAC (with mp4 videos using H.264) or Vorbis (with webm videos using VP8).
* Audio Channels: depends on platform. For example, the Android recommendation is for stereo files, but many devices will accept 5.1.
Android Notes
* Support for resolutions above 640 x 360 is not available on all devices. Runtime checks are done to verify this and failures will cause the movie to not be played.
* For OS 4.1/4.2, movies above 1280 x 720 or with more than 2 audio tracks will not be played due to bugs in the OS libraries.
* For OS 5.0+, any resolution or number of audio channels may be attempted, but will be constrained by device capabilities.
* The Vulkan graphics API is not yet supported.
* Format compatibility issues are reported in the adb logcat
output and are always prefixed with AndroidVideoMedia
.
* Also pay attention to device-specific error messages located near Unity's ones: they are not available to the engine, but often explains what the compatibility issue is.
The following demonstrates a few features of the VideoPlayer:
#pragma strict // Examples of VideoPlayer function public class Example extends MonoBehaviour { function Start() { // Will attach a VideoPlayer to the main camera. var camera: GameObject = GameObject.Find("Main Camera"); // to a camera object, no need to change videoPlayer.targetCamera. var videoPlayer: var = camera.AddComponent.<UnityEngine.Video.VideoPlayer>(); // below to auto-start playback since we're in Start(). videoPlayer.playOnAwake = false; // Let's target the near plane instead. videoPlayer.renderMode = UnityEngine.Video.VideoRenderMode.CameraNearPlane; // This will cause our scene to be visible through the video being played. videoPlayer.targetCameraAlpha = 0.5F; // Here, using absolute. videoPlayer.url = "/Users/graham/movie.mov"; // Skip the first 100 frames. videoPlayer.frame = 100; // Restart from beginning when done. videoPlayer.isLooping = true; // Each time we reach the end, we slow down the playback by a factor of 10. videoPlayer.loopPointReached += EndReached; // its prepareCompleted event. videoPlayer.Play(); } function EndReached(vp: UnityEngine.Video.VideoPlayer) { vp.playbackSpeed = vp.playbackSpeed / 10.0F; } }
// Examples of VideoPlayer function
using UnityEngine;
public class Example : MonoBehaviour { void Start() { // Will attach a VideoPlayer to the main camera. GameObject camera = GameObject.Find("Main Camera");
// VideoPlayer automatically targets the camera backplane when it is added // to a camera object, no need to change videoPlayer.targetCamera. var videoPlayer = camera.AddComponent<UnityEngine.Video.VideoPlayer>();
// Play on awake defaults to true. Set it to false to avoid the url set // below to auto-start playback since we're in Start(). videoPlayer.playOnAwake = false;
// By default, VideoPlayers added to a camera will use the far plane. // Let's target the near plane instead. videoPlayer.renderMode = UnityEngine.Video.VideoRenderMode.CameraNearPlane;
// This will cause our scene to be visible through the video being played. videoPlayer.targetCameraAlpha = 0.5F;
// Set the video to play. URL supports local absolute or relative paths. // Here, using absolute. videoPlayer.url = "/Users/graham/movie.mov";
// Skip the first 100 frames. videoPlayer.frame = 100;
// Restart from beginning when done. videoPlayer.isLooping = true;
// Each time we reach the end, we slow down the playback by a factor of 10. videoPlayer.loopPointReached += EndReached;
// Start playback. This means the VideoPlayer may have to prepare (reserve // resources, pre-load a few frames, etc.). To better control the delays // associated with this preparation one can use videoPlayer.Prepare() along with // its prepareCompleted event. videoPlayer.Play(); }
void EndReached(UnityEngine.Video.VideoPlayer vp) { vp.playbackSpeed = vp.playbackSpeed / 10.0F; } }
controlledAudioTrackMaxCount | Maximum number of audio tracks that can be controlled. |
aspectRatio | Defines how the video content will be stretched to fill the target area. |
audioOutputMode | Destination for the audio embedded in the video. |
audioTrackCount | Number of audio tracks found in the data source currently configured. |
canSetDirectAudioVolume | Whether direct-output volume controls are supported for the current platform and video format. (Read Only) |
canSetPlaybackSpeed | Whether the playback speed can be changed. (Read Only) |
canSetSkipOnDrop | Determines whether the VideoPlayer skips frames to catch up with current time. (Read Only) |
canSetTime | Whether current time can be changed using the time or timeFrames property. (Read Only) |
canSetTimeSource | Whether the time source followed by the VideoPlayer can be changed. (Read Only) |
canStep | Returns true if the VideoPlayer can step forward through the video content. (Read Only) |
clip | The clip being played by the VideoPlayer. |
controlledAudioTrackCount | Number of audio tracks that this VideoPlayer will take control of. The other ones will be silenced. A maximum of 64 tracks are allowed. The actual number of audio tracks cannot be known in advance when playing URLs, which is why this value is independent of the VideoPlayer.audioTrackCount property. |
externalReferenceTime | Reference time of the external clock the VideoPlayer uses to correct its drift. |
frame | The frame index currently being displayed by the VideoPlayer. |
frameCount | Number of frames in the current video content. |
frameRate | The frame rate of the clip or URL in frames/second. (Read Only). |
isLooping | Determines whether the VideoPlayer restarts from the beginning when it reaches the end of the clip. |
isPlaying | Whether content is being played. (Read Only) |
isPrepared | Whether the VideoPlayer has successfully prepared the content to be played. (Read Only) |
playbackSpeed | Factor by which the basic playback rate will be multiplied. |
playOnAwake | Whether the content will start playing back as soon as the component awakes. |
renderMode | Where the video content will be drawn. |
sendFrameReadyEvents | Enables the frameReady events. |
skipOnDrop | Whether the VideoPlayer is allowed to skip frames to catch up with current time. |
source | The source that the VideoPlayer uses for playback. |
targetCamera | Camera component to draw to when VideoPlayer.renderMode is set to either VideoRenderMode.CameraFarPlane or VideoRenderMode.CameraNearPlane. |
targetCamera3DLayout | Type of 3D content contained in the source video media. |
targetCameraAlpha | Overall transparency level of the target camera plane video. |
targetMaterialProperty | Material texture property which is targeted when VideoPlayer.renderMode is set to Video.VideoTarget.MaterialOverride. |
targetMaterialRenderer | Renderer which is targeted when VideoPlayer.renderMode is set to Video.VideoTarget.MaterialOverride |
targetTexture | RenderTexture to draw to when VideoPlayer.renderMode is set to Video.VideoTarget.RenderTexture. |
texture | Internal texture in which video content is placed. |
time | The VideoPlayer current time in seconds. |
timeReference | The clock that the VideoPlayer observes to detect and correct drift. |
timeSource | [NOT YET IMPLEMENTED] The source used used by the VideoPlayer to derive its current time. |
url | The file or HTTP URL that the VideoPlayer will read content from. |
waitForFirstFrame | Determines whether the VideoPlayer will wait for the first frame to be loaded into the texture before starting playback when VideoPlayer.playOnAwake is on. |
EnableAudioTrack | Enable/disable audio track decoding. Only effective when the VideoPlayer is not currently playing. |
GetAudioChannelCount | The number of audio channels in the specified audio track. |
GetAudioLanguageCode | Returns the language code, if any, for the specified track. |
GetDirectAudioMute | Get the direct-output audio mute status for the specified track. |
GetDirectAudioVolume | Return the direct-output volume for specified track. |
GetTargetAudioSource | Gets the AudioSource that will receive audio samples for the specified track if VideoPlayer.audioOutputMode is set to VideoAudioOutputMode.AudioSource. |
IsAudioTrackEnabled | Returns whether decoding for the specified audio track is enabled. See VideoPlayer.EnableAudioTrack for distinction with mute. |
Pause | Pauses the playback and leaves the current time intact. |
Play | Starts playback. |
Prepare | Initiates playback engine prepration. |
SetDirectAudioMute | Set the direct-output audio mute status for the specified track. |
SetDirectAudioVolume | Set the direct-output audio volume for the specified track. |
SetTargetAudioSource | Sets the AudioSource that will receive audio samples for the specified track if this audio target is selected with VideoPlayer.audioOutputMode. |
StepForward | Advances the current time by one frame immediately. |
Stop | Pauses the playback and sets the current time to 0. |
clockResyncOccurred | Invoked when the VideoPlayer's clock is synced back to its VideoTimeReference. |
errorReceived | Errors such as HTTP connection problems are reported through this callback. |
frameDropped | [NOT YET IMPLEMENTED] Invoked when the video decoder does not produce a frame as per the time source during playback. |
frameReady | Invoked when a new frame is ready. |
loopPointReached | Invoked when the VideoPlayer reaches the end of the content to play. |
prepareCompleted | Invoked when the VideoPlayer preparation is complete. |
seekCompleted | Invoke after a seek operation completes. |
started | Invoked immediately after Play is called. |
ErrorEventHandler | Delegate type for VideoPlayer events that contain an error message. |
EventHandler | Delegate type for all parameter-less events emitted by VideoPlayers. |
FrameReadyEventHandler | Delegate type for VideoPlayer events that carry a frame number. |
TimeEventHandler | Delegate type for VideoPlayer events that carry a time position. |
enabled | Enabled Behaviours are Updated, disabled Behaviours are not. |
isActiveAndEnabled | Has the Behaviour had enabled called. |
gameObject | The game object this component is attached to. A component is always attached to a game object. |
tag | The tag of this game object. |
transform | The Transform attached to this GameObject. |
hideFlags | Should the object be hidden, saved with the scene or modifiable by the user? |
name | The name of the object. |
BroadcastMessage | Calls the method named methodName on every MonoBehaviour in this game object or any of its children. |
CompareTag | Is this game object tagged with tag ? |
GetComponent | Returns the component of Type type if the game object has one attached, null if it doesn't. |
GetComponentInChildren | Returns the component of Type type in the GameObject or any of its children using depth first search. |
GetComponentInParent | Returns the component of Type type in the GameObject or any of its parents. |
GetComponents | Returns all components of Type type in the GameObject. |
GetComponentsInChildren | Returns all components of Type type in the GameObject or any of its children. |
GetComponentsInParent | Returns all components of Type type in the GameObject or any of its parents. |
SendMessage | Calls the method named methodName on every MonoBehaviour in this game object. |
SendMessageUpwards | Calls the method named methodName on every MonoBehaviour in this game object and on every ancestor of the behaviour. |
GetInstanceID | Returns the instance id of the object. |
ToString | Returns the name of the GameObject. |
Destroy | Removes a gameobject, component or asset. |
DestroyImmediate | Destroys the object obj immediately. You are strongly recommended to use Destroy instead. |
DontDestroyOnLoad | Makes the object target not be destroyed automatically when loading a new scene. |
FindObjectOfType | Returns the first active loaded object of Type type. |
FindObjectsOfType | Returns a list of all active loaded objects of Type type. |
Instantiate | Clones the object original and returns the clone. |
bool | Does the object exist? |
operator != | Compares if two objects refer to a different object. |
operator == | Compares two object references to see if they refer to the same object. |