Hello guy’s,
I use the script which is given by oculus to play movies on the gear (MoviePlayerSample.cs). Perhaps when I come back to my scene with the video from another, I can’t lunch the video.
Is there someone who already have this problem or can help me to fix this?
Thank’s.
Here my code :
public class GearVRMoviePlayer2 : MonoBehaviour
{
private int nativeTextureID;
private bool startedVideo = false;
public float MovieTime;
public static bool movieIsEnd = false;
private bool movieIsPlaying = false;
private bool PauseVar = false;
private int PauseInt = 0;
private bool TutoEndCall = false;
//public GameObject ThisObj;
public Text debugText;
#if (UNITY_ANDROID && !UNITY_EDITOR)
bool videoPaused = false;
AndroidJavaObject mediaPlayer = null;
#endif
/// <summary>
/// Initialization of the movie surface
/// </summary>
void Awake()
{
//gameObject.transform.Rotate(new Vector3(90,180,0));
if (GetComponent<Renderer>().material == null || GetComponent<Renderer>().material.mainTexture == null)
{
Debug.LogError("Can't GetNativeTextureID() for movie surface");
}
// This aparently has to be done at Awake time, before
// multithreaded rendering starts;
nativeTextureID = GetComponent<Renderer>().material.mainTexture.GetNativeTextureID();
Debug.Log("Movie Texture id: " + nativeTextureID);
}
void Update ()
{
#if (UNITY_ANDROID && !UNITY_EDITOR)
if (Input.GetKeyDown (KeyCode.Escape) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2))
{
mediaPlayer.Call("stop");
StopCoroutine(VideoTimeWait(MovieTime));
mediaPlayer.Call("prepare");
mediaPlayer.Call("setLooping", false);
mediaPlayer.Call("seekTo",0);
movieIsEnd = true;
Application.LoadLevel("Video2");
}
if (Input.GetMouseButtonDown(0))
{
if (PauseInt <1)
{
Invoke("DelayedStartVideo", 0.1f);
movieIsPlaying = true;
StartCoroutine(VideoTimeWait(MovieTime));
}
else if (movieIsEnd == true)
{
movieIsPlaying = true;
StartCoroutine(VideoTimeWait(MovieTime));
mediaPlayer.Call("start");
movieIsEnd = false;
}
else
{
PauseVar = ! PauseVar;
}
PauseInt = PauseInt +1;
}
if (PauseVar == true)
{
PauseTime();
movieIsPlaying = false;
}
else if (movieIsPlaying == false)
{
mediaPlayer.Call("start");
movieIsPlaying = true;
}
//Info.text = movieIsEnd.ToString();
#endif
debugText.text = nativeTextureID.ToString () + " actu : " + GetComponent<Renderer> ().material.mainTexture.GetNativeTextureID ().ToString ();
}
/// <summary>
/// Auto-starts video playback
/// </summary>
void DelayedStartVideo()
{
if (!startedVideo)
{
startedVideo = true;
#if (UNITY_ANDROID && !UNITY_EDITOR)
// This can only be done once multithreaded rendering is running
mediaPlayer = StartVideoPlayerOnTextureId(nativeTextureID);
#endif
}
}
void Start()
{
#if (UNITY_ANDROID && !UNITY_EDITOR)
// delay one frame because OVRCameraController initializes the render thread in Start()
//Invoke("DelayedStartVideo", 0.1f);
#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)
{
videoPaused = wasPaused;
mediaPlayer.Call(( videoPaused ) ? "pause" : "start");
}
#endif
}
#if (UNITY_ANDROID && !UNITY_EDITOR)
void PauseTime()
{
mediaPlayer.Call("pause");
}
IEnumerator VideoTimeWait (float VideoTime)
{
yield return new WaitForSeconds (VideoTime);
//movieIsPlaying = false;
//PauseInt = 0;
movieIsEnd = true;
}
#endif
#if (UNITY_ANDROID && !UNITY_EDITOR)
// This function returns an Android Surface object that is
// bound to a SurfaceTexture object on an independent OpenGL texture id.
// Each frame, before the TimeWarp processing, the SurfaceTexture is checked
// for updates, and if one is present, the contents of the SurfaceTexture
// will be copied over to the provided surfaceTexId and mipmaps will be
// generated so normal Unity rendering can use it.
[DllImport("OculusPlugin")]
private static extern IntPtr OVR_Media_Surface(int surfaceTexId);
#endif
#if (UNITY_ANDROID && !UNITY_EDITOR)
/// <summary>
/// Set up the video player with the movie surface texture id
/// </summary>
AndroidJavaObject StartVideoPlayerOnTextureId(int textureId)
{
Debug.Log("SetUpVideoPlayer ");
IntPtr androidSurface = OVR_Media_Surface(textureId);
AndroidJavaObject mediaPlayer = new AndroidJavaObject("android/media/MediaPlayer");
// Can't use AndroidJavaObject.Call() with a jobject, must use low level interface
//mediaPlayer.Call("setSurface", androidSurface);
IntPtr setSurfaceMethodId = AndroidJNI.GetMethodID(mediaPlayer.GetRawClass(),"setSurface","(Landroid/view/Surface;)V");
jvalue[] parms = new jvalue[1];
parms[0] = new jvalue();
parms[0].l = androidSurface;
AndroidJNI.CallObjectMethod(mediaPlayer.GetRawObject(), setSurfaceMethodId, parms);
mediaPlayer.Call("setDataSource", "/storage/extSdCard/Oculus/Movies/Trailers/ShortVr.mp4");
mediaPlayer.Call("prepare");
mediaPlayer.Call("setLooping", false);
mediaPlayer.Call("start");
return mediaPlayer;
}
#endif
}