192 lines
3.8 KiB
C#
192 lines
3.8 KiB
C#
using System;
|
|
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.Video;
|
|
|
|
public class MoviePlayerSample : MonoBehaviour
|
|
{
|
|
private bool videoPausedBeforeAppPause;
|
|
|
|
private VideoPlayer videoPlayer;
|
|
|
|
private OVROverlay overlay;
|
|
|
|
private Renderer mediaRenderer;
|
|
|
|
private RenderTexture copyTexture;
|
|
|
|
private Material externalTex2DMaterial;
|
|
|
|
public string MovieName;
|
|
|
|
public bool isPlaying { get; private set; }
|
|
|
|
private void Awake()
|
|
{
|
|
Debug.Log("MovieSample Awake");
|
|
mediaRenderer = GetComponent<Renderer>();
|
|
videoPlayer = GetComponent<VideoPlayer>();
|
|
if (videoPlayer == null)
|
|
{
|
|
videoPlayer = base.gameObject.AddComponent<VideoPlayer>();
|
|
}
|
|
overlay = GetComponent<OVROverlay>();
|
|
if (overlay == null)
|
|
{
|
|
overlay = base.gameObject.AddComponent<OVROverlay>();
|
|
}
|
|
overlay.currentOverlayShape = OVROverlay.OverlayShape.Equirect;
|
|
overlay.overrideTextureRectMatrix = true;
|
|
overlay.SetSrcDestRects(new Rect(0f, 0f, 0.5f, 1f), new Rect(0.5f, 0f, 0.5f, 1f), new Rect(0.25f, 0f, 0.5f, 1f), new Rect(0.25f, 0f, 0.5f, 1f));
|
|
overlay.enabled = false;
|
|
overlay.isExternalSurface = NativeVideoPlayer.IsAvailable;
|
|
overlay.enabled = Application.platform == RuntimePlatform.Android;
|
|
}
|
|
|
|
private IEnumerator Start()
|
|
{
|
|
if (mediaRenderer.material == null)
|
|
{
|
|
Debug.LogError("No material for movie surface");
|
|
yield break;
|
|
}
|
|
yield return new WaitForSeconds(1f);
|
|
if (!string.IsNullOrEmpty(MovieName))
|
|
{
|
|
Play(Application.streamingAssetsPath + "/" + MovieName);
|
|
}
|
|
}
|
|
|
|
public void Play(string moviePath)
|
|
{
|
|
if (moviePath != string.Empty)
|
|
{
|
|
Debug.Log("Playing Video: " + moviePath);
|
|
if (overlay.isExternalSurface)
|
|
{
|
|
OVROverlay.ExternalSurfaceObjectCreated externalSurfaceObjectCreated = delegate
|
|
{
|
|
Debug.Log("Playing ExoPlayer with SurfaceObject");
|
|
NativeVideoPlayer.PlayVideo(moviePath, overlay.externalSurfaceObject);
|
|
};
|
|
if (overlay.externalSurfaceObject == IntPtr.Zero)
|
|
{
|
|
overlay.externalSurfaceObjectCreated = externalSurfaceObjectCreated;
|
|
}
|
|
else
|
|
{
|
|
externalSurfaceObjectCreated();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.Log("Playing Unity VideoPlayer");
|
|
videoPlayer.url = moviePath;
|
|
videoPlayer.Prepare();
|
|
videoPlayer.Play();
|
|
}
|
|
Debug.Log("MovieSample Start");
|
|
isPlaying = true;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("No media file name provided");
|
|
}
|
|
}
|
|
|
|
public void Play()
|
|
{
|
|
if (overlay.isExternalSurface)
|
|
{
|
|
NativeVideoPlayer.Play();
|
|
}
|
|
else
|
|
{
|
|
videoPlayer.Play();
|
|
}
|
|
isPlaying = true;
|
|
}
|
|
|
|
public void Pause()
|
|
{
|
|
if (overlay.isExternalSurface)
|
|
{
|
|
NativeVideoPlayer.Pause();
|
|
}
|
|
else
|
|
{
|
|
videoPlayer.Pause();
|
|
}
|
|
isPlaying = false;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (overlay.isExternalSurface)
|
|
{
|
|
return;
|
|
}
|
|
Texture texture = ((!(videoPlayer.texture != null)) ? Texture2D.blackTexture : videoPlayer.texture);
|
|
if (overlay.enabled)
|
|
{
|
|
if (overlay.textures[0] != texture)
|
|
{
|
|
overlay.enabled = false;
|
|
overlay.textures[0] = texture;
|
|
overlay.enabled = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
mediaRenderer.material.mainTexture = texture;
|
|
mediaRenderer.material.SetVector("_SrcRectLeft", overlay.srcRectLeft.ToVector());
|
|
mediaRenderer.material.SetVector("_SrcRectRight", overlay.srcRectRight.ToVector());
|
|
}
|
|
}
|
|
|
|
public void Rewind()
|
|
{
|
|
if (overlay.isExternalSurface)
|
|
{
|
|
NativeVideoPlayer.SetPlaybackSpeed(-1f);
|
|
}
|
|
else
|
|
{
|
|
videoPlayer.playbackSpeed = -1f;
|
|
}
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
if (overlay.isExternalSurface)
|
|
{
|
|
NativeVideoPlayer.Stop();
|
|
}
|
|
else
|
|
{
|
|
videoPlayer.Stop();
|
|
}
|
|
isPlaying = false;
|
|
}
|
|
|
|
private void OnApplicationPause(bool appWasPaused)
|
|
{
|
|
Debug.Log("OnApplicationPause: " + appWasPaused);
|
|
if (appWasPaused)
|
|
{
|
|
videoPausedBeforeAppPause = !isPlaying;
|
|
}
|
|
if (!videoPausedBeforeAppPause)
|
|
{
|
|
if (appWasPaused)
|
|
{
|
|
Pause();
|
|
}
|
|
else
|
|
{
|
|
Play();
|
|
}
|
|
}
|
|
}
|
|
}
|