Version: 2017.1

VideoPlayer

class in UnityEngine.Video

/

继承自:Behaviour

切换到手册

描述

将视频内容播放到目标上。

内容可以是 VideoClip 导入资源,也可以是 URL(例如 file://http://.)。视频内容将投射到一个受支持的目标上,例如摄像机背景或 RenderTexture。 如果视频内容包括透明度,此透明度将出现在目标中,允许视频目标后面的对象可见。

电影文件格式支持说明

VideoPlayer 在其实现中使用原生音频和视频解码库,您有责任使用符合目标平台要求的视频。VideoClipImporter 提供了一个将 VideoClip 资源转码为 H.264VP8 视频编解码器的选项,以及一些实验性选项(例如 Resolution)。这会将匹配的编解码器用于音频轨道:分别是 AACVorbis

另请参阅:VideoClipImporter.SetTargetSettingsVideoImporterTargetSettings.enableTranscoding

您可以选择忽略此转码,而是改用已知受目标系统支持的视频,同时使用外部程序更精确地控制编码过程。在使用过程中,VideoClipImporter 编辑器会提供指导和警告,以更好地帮助您做出正确的格式和编码选择。

目前,必须遵循供应商建议,而且在旧版移动平台上尤其受到约束。例如,您在 Web 中找到的视频通常需要经过检查和相关操作,才能在运行在多个设备上的游戏环境中可靠地使用。以下是一些建议和已知限制的示例:

* Android支持的媒体格式:。请参阅下方的附加说明。\ * WindowsH.264 视频解码器(请参阅“格式约束”)\ * iPhone 6-7比较 iPhone 型号(请参阅“电视和视频”)

可用于硬件加速的本机支持的最优视频编解码器是 H.264,同时 VP8 作为可以在需要时使用的软件解码解决方案。在 Android 上,VP8 还支持使用本机库,因此也可以根据型号对硬件进行辅助。要在编码参数中查找的键值:

* Video Codec:H.264 或 VP8。\ * Resolution:例如:1280 x 720。\ * Profile:适用于 H.264。配置文件是一组功能和约束;供应商通常会指定此配置文件,例如“Baseline”或“Main”。请参阅here.\ * Profile Level:适用于 H.264。在一个给定配置文件中,级别会指定某些性能要求,例如“Baseline 3.1”。请参阅here.\ * Audio Codec:通常是 AAC(适用于使用 H.264 的 mp4 视频)或 Vorbis(适用于使用 VP8 的 webm 视频)。\ * Audio Channels:取决于平台。例如,针对 Android 的建议是立体声文件,但很多设备都接受 5.1。

关于 Android 的注意事项

* 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.

以下部分演示了 VideoPlayer 的一些功能:

// 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可以控制的最大音频轨道数量。

变量

aspectRatio定义如何拉伸视频内容以填充目标区域。
audioOutputMode嵌入在视频中的音频的目标。
audioTrackCount在当前配置的数据源中找到的音频轨道数量。
canSetDirectAudioVolume当前平台和视频格式是否支持直接输出音量控制。(只读)
canSetPlaybackSpeed播放速度是否可以更改。(只读)
canSetSkipOnDrop确定是否要 VideoPlayer 跳帧以追上当前时间。(只读)
canSetTime是否可以使用 time 或 timeFrames 属性更改当前时间。(只读)
canSetTimeSource是否可以更改 VideoPlayer 遵循的时间源。(只读)
canStep如果 VideoPlayer 可以使视频内容前进,则返回 true。(只读)
clipVideoPlayer 正在播放的剪辑。
controlledAudioTrackCount此 VideoPlayer 将控制的音频轨道数量。其他音频轨道将被调为静音。最多允许 64 个音频轨道。 在播放 URL 时,无法预先知道音频轨道的实际数量,这就是此值独立于 VideoPlayer.audioTrackCount 属性的原因。
externalReferenceTime VideoPlayer 用于纠正其偏差的外部时钟的参考时间。
frameVideoPlayer 当前显示的帧索引。
frameCount当前视频内容中的帧数。
frameRate剪辑或 URL 的帧率(以帧或秒为单位)。(只读)。
isLooping确定当 VideoPlayer 到达剪辑的结尾时是否从头开始播放。
isPlaying是否正在播放内容。(只读)
isPreparedVideoPlayer 是否已完成待播放内容的准备工作。(只读)
playbackSpeed基本播放速率的增加倍数。
playOnAwake内容是否会在组件被唤醒后立即开始播放。
renderMode将绘制视频内容的位置。
sendFrameReadyEvents启用 frameReady 事件。
skipOnDrop是否允许 VideoPlayer 跳帧以追上当前时间。
sourceVideoPlayer 用于播放的源。
targetCameraCamera component to draw to when VideoPlayer.renderMode is set to either Video.VideoTarget.CameraFarPlane or Video.VideoTarget.CameraNearPlane.
targetCameraAlpha目标摄像机平面视频的整体透明度级别。
targetMaterialProperty在 VideoPlayer.renderMode 设置为 Video.VideoTarget.MaterialOverride 时被设为目标的材质纹理属性。
targetMaterialRenderer在 VideoPlayer.renderMode 设置为 Video.VideoTarget.MaterialOverride 时被设为目标的 Renderer
targetTexture RenderTexture to draw to when VideoPlayer.renderMode is set to Video.VideoTarget.RenderTexture. For optimal performance, the dimensions of the RenderTexture should match those of the video media exactly.
texture放置视频内容的内部纹理。
timeVideoPlayer 当前时间(以秒为单位)。
timeReference VideoPlayer 对其进行观测以发现和纠正偏差的时钟。
timeSource[尚未实现] VideoPlayer 用于派生其当前时间的源。
urlVideoPlayer 将从中读取内容的文件或 HTTP URL。
waitForFirstFrame确定当 VideoPlayer.playOnAwake 启用时,VideoPlayer 是否会在开始播放之前等待第一帧加载到纹理中。

公共函数

EnableAudioTrack启用/禁用音频轨道解码。仅在 VideoPlayer 当前未播放时有效。
GetAudioChannelCount指定音频轨道中的音频声道数。
GetAudioLanguageCode返回指定轨道的语言代码(如果有)。
GetDirectAudioMute获取指定轨道的直接输出音频静音状态。
GetDirectAudioVolume返回指定轨道的直接输出音量。
GetTargetAudioSource获取当 VideoPlayer.audioOutputMode 设置为 VideoAudioOutputMode.AudioSource 时将接收指定轨道的音频样本的 AudioSource。
IsAudioTrackEnabled返回是否启用了对指定音频轨道的解码。请参阅 VideoPlayer.EnableAudioTrack 以了解它与静音的区别。
Pause暂停播放并保持当前时间不变。
Play开始播放。
Prepare启动播放引擎准备。
SetDirectAudioMute设置指定轨道的直接输出音频静音状态。
SetDirectAudioVolume设置指定轨道的直接输出音频音量。
SetTargetAudioSource设置当使用 VideoPlayer.audioOutputMode 选择此音频目标时将接收指定轨道的音频样本的 AudioSource。
StepForward立即将当前时间向前推进一帧。
Stop暂停播放并将当前时间设为 0。

Events

clockResyncOccurred在 VideoPlayer 的时钟同步回其 VideoTimeReference 时调用。
errorReceived通过此回调报告 HTTP 连接问题等错误。
frameDropped[尚未实现] 当视频解码器在播放期间没有按照时间源生成帧时调用。
frameReady当新帧准备就绪时调用。
loopPointReached当 VideoPlayer 到达播放内容的结尾时调用。
prepareCompleted当 VideoPlayer 的准备工作完成后调用。
seekCompleted在搜寻操作完成后调用。
started在调用 Play 后立即调用。

委托

ErrorEventHandler包含错误消息的 VideoPlayer 事件的委托类型。
EventHandlerVideoPlayer 发出的所有无参数事件的委托类型。
FrameReadyEventHandler带有帧编号的 VideoPlayer 事件的委托类型。
TimeEventHandler带有时间位置的 VideoPlayer 事件的委托类型。

继承的成员

变量

enabled启用的 Behaviour 可更新,禁用的 Behaviour 不可更新。
isActiveAndEnabled已调用启用的 Behaviour。
gameObject此组件附加到的游戏对象。始终将组件附加到游戏对象。
tag此游戏对象的标签。
transform附加到此 GameObject 的 Transform。
hideFlags该对象应该隐藏、随场景一起保存还是由用户修改?
name对象的名称。

公共函数

BroadcastMessage调用此游戏对象或其任何子项中的每个 MonoBehaviour 上名为 methodName 的方法。
CompareTag此游戏对象是否使用 tag 进行了标记?
GetComponent如果游戏对象附加了类型为 type 的组件,则将其返回,否则返回 null。
GetComponentInChildren使用深度首次搜索返回 GameObject 或其任何子项中类型为 type 的组件。
GetComponentInParent返回 GameObject 或其任何父项中类型为 type 的组件。
GetComponents返回 GameObject 中类型为 type 的所有组件。
GetComponentsInChildren返回 GameObject 或其任何子项中类型为 type 的所有组件。
GetComponentsInParent返回 GameObject 或其任何父项中类型为 type 的所有组件。
SendMessage调用此游戏对象中的每个 MonoBehaviour 上名为 methodName 的方法。
SendMessageUpwards调用此游戏对象中的每个 MonoBehaviour 上或此行为的每个父级上名为 methodName 的方法。
GetInstanceID返回对象的实例 ID。
ToStringReturns the name of the game object.

静态函数

Destroy删除 GameObject、组件或资源。
DestroyImmediate立即销毁对象 /obj/。强烈建议您改用 Destroy。
DontDestroyOnLoad加载新场景时,不自动销毁对象 /target/。
FindObjectOfType返回第一个类型为 type 的已加载的激活对象。
FindObjectsOfType返回所有类型为 type 的已加载的激活对象的列表。
Instantiate克隆 original 对象并返回克隆对象。

运算符

bool该对象是否存在?
operator !=比较两个对象是否引用不同的对象。
operator ==比较两个对象引用,判断它们是否引用同一个对象。