Skip to content

Instantly share code, notes, and snippets.

@niusounds
Created March 11, 2016 12:12
Show Gist options
  • Select an option

  • Save niusounds/8e955276006bdfc2f543 to your computer and use it in GitHub Desktop.

Select an option

Save niusounds/8e955276006bdfc2f543 to your computer and use it in GitHub Desktop.
GearVRVideoPlayer.csを反転球のオブジェクトに付けて、動画のパスを指定する。
using UnityEngine;
using System;
using System.Collections;
using System.Runtime.InteropServices;
public class VideoPlayer: MonoBehaviour {
public string videoPath;
public bool autoPlay = true;
#if (UNITY_ANDROID && !UNITY_EDITOR)
private AndroidJavaObject mediaPlayer;
#else
private MovieTexture movieTexture;
#endif
private Renderer mediaRenderer = null;
private enum MediaSurfaceEventType
{
Initialize = 0,
Shutdown = 1,
Update = 2,
Max_EventType
};
/// <summary>
/// The start of the numeric range used by event IDs.
/// </summary>
/// <description>
/// If multiple native rundering plugins are in use, the Oculus Media Surface plugin's event IDs
/// can be re-mapped to avoid conflicts.
///
/// Set this value so that it is higher than the highest event ID number used by your plugin.
/// Oculus Media Surface plugin event IDs start at eventBase and end at eventBase plus the highest
/// value in MediaSurfaceEventType.
/// </description>
public static int eventBase
{
get { return _eventBase; }
set
{
_eventBase = value;
#if (UNITY_ANDROID && !UNITY_EDITOR)
OVR_Media_Surface_SetEventBase(_eventBase);
#endif
}
}
private static int _eventBase = 0;
private static void IssuePluginEvent(MediaSurfaceEventType eventType)
{
GL.IssuePluginEvent((int)eventType + eventBase);
}
/// <summary>
/// Initialization of the movie surface
/// </summary>
void Awake() {
// Common
mediaRenderer = GetComponent<Renderer>();
#if (UNITY_ANDROID && !UNITY_EDITOR)
OVR_InitMediaSurface();
//IssuePluginEvent(MediaSurfaceEventType.Initialize);
mediaPlayer = new AndroidJavaObject("android/media/MediaPlayer");
#endif
StartCoroutine(AutoPrepare());
}
IEnumerator AutoPrepare()
{
yield return null; // delay 1 frame to allow MediaSurfaceInit from the render thread.
if (mediaRenderer.sharedMaterial.mainTexture != null)
{
setTargetTexture(mediaRenderer.sharedMaterial.mainTexture, 4096, 2048);
if (videoPath != string.Empty)
{
yield return load(videoPath);
if (autoPlay)
{
start();
}
}
}
}
public void setTargetTexture(Texture texture, int width, int height)
{
Debug.Log("setTargetTexture");
#if (UNITY_ANDROID && !UNITY_EDITOR)
IntPtr textureId = texture.GetNativeTexturePtr();
IntPtr androidSurface = OVR_Media_Surface(textureId, width, height);
setSurface(androidSurface);
#endif
}
public void setSurface(IntPtr surfacePtr)
{
Debug.Log("setSurface");
#if (UNITY_ANDROID && !UNITY_EDITOR)
IntPtr setSurfaceMethodId = AndroidJNI.GetMethodID(mediaPlayer.GetRawClass(), "setSurface", "(Landroid/view/Surface;)V");
jvalue[] parms = new jvalue[1];
parms[0] = new jvalue();
parms[0].l = surfacePtr;
AndroidJNI.CallVoidMethod(mediaPlayer.GetRawObject(), setSurfaceMethodId, parms);
#endif
}
public IEnumerator load(string path)
{
Debug.Log("load");
#if (UNITY_ANDROID && !UNITY_EDITOR)
mediaPlayer.Call("setDataSource", path);
mediaPlayer.Call("prepare");
return null;
#else
WWW www = new WWW("file://" + path);
yield return www;
if (www.error !=null)
{
Debug.LogError(www.error);
}
movieTexture = www.movie;
mediaRenderer.sharedMaterial.mainTexture = movieTexture;
#endif
}
public void start()
{
#if (UNITY_ANDROID && !UNITY_EDITOR)
mediaPlayer.Call("start");
#else
movieTexture.Play();
#endif
}
public void pause()
{
#if (UNITY_ANDROID && !UNITY_EDITOR)
mediaPlayer.Call("pause");
#else
movieTexture.Pause();
#endif
}
public void setLooping(bool val)
{
#if (UNITY_ANDROID && !UNITY_EDITOR)
mediaPlayer.Call("setLooping", val);
#else
movieTexture.loop = val;
#endif
}
public void setkTo(int msec)
{
#if (UNITY_ANDROID && !UNITY_EDITOR)
mediaPlayer.Call("seekTo", msec);
#endif
}
// Update is called once per frame
void Update () {
#if (UNITY_ANDROID && !UNITY_EDITOR)
IssuePluginEvent(MediaSurfaceEventType.Update);
#endif
}
/// <summary>
/// Pauses video playback when the app loses or gains focus
/// </summary>
void OnApplicationPause(bool wasPaused)
{
Debug.Log("OnApplicationPause: " + wasPaused);
#if (UNITY_ANDROID && !UNITY_EDITOR)
if (mediaPlayer != null)
{
try {
if (wasPaused) {
pause();
} else {
start();
}
} catch (Exception e) {
Debug.Log("Failed to start/pause mediaPlayer with message " + e.Message);
}
}
#endif
}
void OnDestroy()
{
#if (UNITY_ANDROID && !UNITY_EDITOR)
OVR_ShutdownMediaSurface();
#endif
}
// libOculusMediaSurface.so
#if (UNITY_ANDROID && !UNITY_EDITOR)
[DllImport("OculusMediaSurface")]
private static extern void OVR_InitMediaSurface();
[DllImport("OculusMediaSurface")]
private static extern void OVR_Media_Surface_SetEventBase(int eventBase);
[DllImport("OculusMediaSurface")]
private static extern void OVR_ShutdownMediaSurface();
[DllImport("OculusMediaSurface")]
private static extern IntPtr OVR_Media_Surface(IntPtr texPtr, int width, int height);
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment