Stop Video Playback
Hallo
1. SDK Version: 3.5.2
2. Development Environment: Unity-Android/IOS
3. Tracker/Scanner:Image Tracker
4. License Type: Free
I have some troble with Video, here they are.
First i add as child Plane add to it VIdeo Player - Video yes Audio not (Solve - In component Video Player at the bottom Audio Source chose my video. done)
Second i press play and with start application starts Video (Solve - I ancheck box "Play on Awake" and add in ImageTrackableBehaviour , in OnTrackSuccess method next strings
VideoPlayer videoPlayer = GetComponentInChildren<VideoPlayer>();
videoPlayer.Play();
Done. Video plays only when Imange is found)
Third it's where i stock. Video continiuse to play when target is lost. I was trying to add the same strings with videoPlayer.Stop(); to OnTrackFail() method, but i get error
NullReferenceException: Object reference not set to an instance of an object
maxstAR.ImageTrackableBehaviour.OnTrackFail () (at Assets/MaxstAR/Script/ImageTrackableBehaviour.cs:71) (It's where i did changes)
ImageTrackerSample.DisableAllTrackables () (at Assets/MaxstARSamples/Scripts/ImageTrackerSample.cs:81)
ImageTrackerSample.Update () (at Assets/MaxstARSamples/Scripts/ImageTrackerSample.cs:87)
Cane you help me.
Hi,
The error log that you post is for "Null pointer error".
Did you declare the 'videoPlayer' as class variable or local variable?
It looks like that videoPlayer variable was not initialized properly.
If my guess is not right please reply to here.
Thank you
Jack, SDK support team
Maybe, but why then videoPlayer.Play comand works fine? And if i add comand videoPlayer.Stop all crashes. Even camera stop turn on. Maybe becose i do it in ImageTrackableBehaviour?
using UnityEngine;
using UnityEngine.Video;
using System.IO;
using JsonFx.Json;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using UnityEngine.Rendering;
namespace maxstAR
{
public class ImageTrackableBehaviour : AbstractImageTrackableBehaviour
{
public static VideoPlayer videoPlayer;
public override void OnTrackSuccess(string id, string name, Matrix4x4 poseMatrix)
{
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
// Enable renderers
foreach (Renderer component in rendererComponents)
{
component.enabled = true;
}
// Enable colliders
foreach (Collider component in colliderComponents)
{
component.enabled = true;
}
transform.position = MatrixUtils.PositionFromMatrix(poseMatrix);
transform.rotation = MatrixUtils.QuaternionFromMatrix(poseMatrix);
StartPlaying();
}
public override void OnTrackFail()
{
videoPlayer.Stop();
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
// Disable renderer
foreach (Renderer component in rendererComponents)
{
component.enabled = false;
}
//Disable collider
foreach (Collider component in colliderComponents)
{
component.enabled = false;
}
}
public void StartPlaying()
{
videoPlayer = GetComponentInChildren<VideoPlayer>(true);
videoPlayer.Play();
}
}
}
Your program can not get videoPlayer instance until tracking success.
So, please initialize videoPlayer variable in Start function.
void Start() {
videoPlayer = GetComponentInChildren<VideoPlayer>(true);
}
And you don't need declare videoPlayer variable as static.
Still not. I do something wrong. But if i coment line videoPlayer.Stop(); all fine, camera tirning on.
NullReferenceException: Object reference not set to an instance of an object
maxstAR.ImageTrackableBehaviour.OnTrackFail () (at Assets/MaxstAR/Script/ImageTrackableBehaviour.cs:75)
ImageTrackerSample.DisableAllTrackables () (at Assets/MaxstARSamples/Scripts/ImageTrackerSample.cs:81)
ImageTrackerSample.Update () (at Assets/MaxstARSamples/Scripts/ImageTrackerSample.cs:87)
using UnityEngine;
using UnityEngine.Video;
using System.IO;
using JsonFx.Json;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using UnityEngine.Rendering;
namespace maxstAR
{
public class ImageTrackableBehaviour : AbstractImageTrackableBehaviour
{
public VideoPlayer videoPlayer;
void Start()
{
videoPlayer = GetComponentInChildren<VideoPlayer>(true);
}
public override void OnTrackSuccess(string id, string name, Matrix4x4 poseMatrix)
{
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
// Enable renderers
foreach (Renderer component in rendererComponents)
{
component.enabled = true;
}
// Enable colliders
foreach (Collider component in colliderComponents)
{
component.enabled = true;
}
transform.position = MatrixUtils.PositionFromMatrix(poseMatrix);
transform.rotation = MatrixUtils.QuaternionFromMatrix(poseMatrix);
videoPlayer.Play();
}
public override void OnTrackFail()
{
Renderer[] rendererComponents = GetComponentsInChildren<Renderer>(true);
Collider[] colliderComponents = GetComponentsInChildren<Collider>(true);
// Disable renderer
foreach (Renderer component in rendererComponents)
{
component.enabled = false;
}
//Disable collider
foreach (Collider component in colliderComponents)
{
component.enabled = false;
}
videoPlayer.Stop();
}
}
}
It's very weird thing that you said.
We add VideoPlayer sample scene our github sample. Here is the url
https://github.com/maxstdev/MaxstARSDK_Unity_Sample
You should find sample scene in Assets/ExtraSamples/Scenes folder and scene name is ImageTrackerVideoPlayer.unity.
Works great.
Thanks.
One more thing. To add audio you need to add Audio Source component below Video Player and drug Video Player at Audio Source in Video Player
I have one mo problem. I need that when target lost video don't pause, I need that video Stops(becouse I have many of them and if they all will be buffered there will be crash) But when I chenge videoPlayer.Pause(); to videoPlayer.Stop(); I create problem. App starts and video shows but it do not play even if i check box Play On Awake.
Cane you see why. Coz if i gona hasle in your code, i will not make it better, not working maybe.
Thank you very much.
Hi, I updated VideoPlayer sample to use Stop() and Prepair() instead of Pause().
However the sample has a problem when tracking succeeds and you can find what it is easily.
The problem is caused by continuous function call of Stop() and Prepare().
I think you can get the video controlling by remembering previous tracking ids.
Thank you