151 lines
3.4 KiB
C#
151 lines
3.4 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
public static class NativeVideoPlayer
|
|
{
|
|
private static IntPtr? _Activity;
|
|
|
|
private static IntPtr? _VideoPlayerClass;
|
|
|
|
private static IntPtr playVideoMethodId;
|
|
|
|
private static IntPtr stopMethodId;
|
|
|
|
private static IntPtr resumeMethodId;
|
|
|
|
private static IntPtr pauseMethodId;
|
|
|
|
private static IntPtr setPlaybackSpeedMethodId;
|
|
|
|
private static IntPtr VideoPlayerClass
|
|
{
|
|
get
|
|
{
|
|
if (!_VideoPlayerClass.HasValue)
|
|
{
|
|
try
|
|
{
|
|
IntPtr intPtr = AndroidJNI.FindClass("com/oculus/videoplayer/MyVideoPlayer");
|
|
if (intPtr != IntPtr.Zero)
|
|
{
|
|
_VideoPlayerClass = AndroidJNI.NewGlobalRef(intPtr);
|
|
AndroidJNI.DeleteLocalRef(intPtr);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError("Failed to find MyVideoPlayer class");
|
|
_VideoPlayerClass = IntPtr.Zero;
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Debug.LogError("Failed to find MyVideoPlayer class");
|
|
Debug.LogException(exception);
|
|
_VideoPlayerClass = IntPtr.Zero;
|
|
}
|
|
}
|
|
return _VideoPlayerClass.GetValueOrDefault();
|
|
}
|
|
}
|
|
|
|
private static IntPtr Activity
|
|
{
|
|
get
|
|
{
|
|
if (!_Activity.HasValue)
|
|
{
|
|
try
|
|
{
|
|
IntPtr intPtr = AndroidJNI.FindClass("com/unity3d/player/UnityPlayer");
|
|
IntPtr staticFieldID = AndroidJNI.GetStaticFieldID(intPtr, "currentActivity", "Landroid/app/Activity;");
|
|
IntPtr staticObjectField = AndroidJNI.GetStaticObjectField(intPtr, staticFieldID);
|
|
_Activity = AndroidJNI.NewGlobalRef(staticObjectField);
|
|
AndroidJNI.DeleteLocalRef(staticObjectField);
|
|
AndroidJNI.DeleteLocalRef(intPtr);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Debug.LogException(exception);
|
|
_Activity = IntPtr.Zero;
|
|
}
|
|
}
|
|
return _Activity.GetValueOrDefault();
|
|
}
|
|
}
|
|
|
|
public static bool IsAvailable
|
|
{
|
|
get
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static void PlayVideo(string path, IntPtr surfaceObj)
|
|
{
|
|
if (playVideoMethodId == IntPtr.Zero)
|
|
{
|
|
playVideoMethodId = AndroidJNI.GetStaticMethodID(VideoPlayerClass, "playVideo", "(Landroid/content/Context;Ljava/lang/String;Landroid/view/Surface;)V");
|
|
}
|
|
IntPtr intPtr = AndroidJNI.NewStringUTF(path);
|
|
AndroidJNI.CallStaticVoidMethod(VideoPlayerClass, playVideoMethodId, new jvalue[3]
|
|
{
|
|
new jvalue
|
|
{
|
|
l = Activity
|
|
},
|
|
new jvalue
|
|
{
|
|
l = intPtr
|
|
},
|
|
new jvalue
|
|
{
|
|
l = surfaceObj
|
|
}
|
|
});
|
|
AndroidJNI.DeleteLocalRef(intPtr);
|
|
}
|
|
|
|
public static void Stop()
|
|
{
|
|
if (stopMethodId == IntPtr.Zero)
|
|
{
|
|
stopMethodId = AndroidJNI.GetStaticMethodID(VideoPlayerClass, "stop", "()V");
|
|
}
|
|
AndroidJNI.CallStaticVoidMethod(VideoPlayerClass, stopMethodId, new jvalue[0]);
|
|
}
|
|
|
|
public static void Play()
|
|
{
|
|
if (resumeMethodId == IntPtr.Zero)
|
|
{
|
|
resumeMethodId = AndroidJNI.GetStaticMethodID(VideoPlayerClass, "resume", "()V");
|
|
}
|
|
AndroidJNI.CallStaticVoidMethod(VideoPlayerClass, resumeMethodId, new jvalue[0]);
|
|
}
|
|
|
|
public static void Pause()
|
|
{
|
|
if (pauseMethodId == IntPtr.Zero)
|
|
{
|
|
pauseMethodId = AndroidJNI.GetStaticMethodID(VideoPlayerClass, "pause", "()V");
|
|
}
|
|
AndroidJNI.CallStaticVoidMethod(VideoPlayerClass, pauseMethodId, new jvalue[0]);
|
|
}
|
|
|
|
public static void SetPlaybackSpeed(float speed)
|
|
{
|
|
if (setPlaybackSpeedMethodId == IntPtr.Zero)
|
|
{
|
|
setPlaybackSpeedMethodId = AndroidJNI.GetStaticMethodID(VideoPlayerClass, "setPlaybackSpeed", "(f)V");
|
|
}
|
|
AndroidJNI.CallStaticVoidMethod(VideoPlayerClass, setPlaybackSpeedMethodId, new jvalue[1]
|
|
{
|
|
new jvalue
|
|
{
|
|
f = speed
|
|
}
|
|
});
|
|
}
|
|
}
|