VideoPlayer stops playing after switching out of the app and back again

Hi everyone,
Is this a bug? Do I need to listen OnApplicationFocus in AVP? and play the VideoPlayer after switching back to the application?

Hi!

Do you mean the normal Unity VideoPlayer or the VisionOSVideoComponent? I tried reproing both in RealityKit simulator and was not able to see the behavior described. That being said, attaching an event to OnWindowEvent in VolumeCamera might be one way to listen for changes in app volume state (going from unfocused to focused for example), and you can manually call VideoPlayer::Play() from there. Make sure that, for the normal Unity VideoPlayer, you are also calling PolySpatialObjectUtils.MarkDirty() on the render texture.

If the issue persists, please submit a bug report and a repro project with repro steps. Thank you!

It’s normal Unity VideoPlayer, I had called PolySpatialObjectUtils.MarkDirty() on the render texture. Only when you leave and return to the app will the video stop playing.

I will test OnWindowEvent to replay videoplayer

After my testing, the video cannot be replayed through OnWindowEvent.

this is my code:

using System;
using Unity.PolySpatial;
#if !UNITY_EDITOR
using Unity.PolySpatial;
#endif
using UnityEngine;
using UnityEngine.Video;

namespace Onesight.Runtime
{
    [RequireComponent ( typeof( VideoPlayer ) )]
    public class VideoPlayerOnPolySpatial : MonoBehaviour
    {
        private void OnDestroy ()
        {
            if ( volumeCamera )
            {
                volumeCamera.OnWindowEvent.RemoveListener ( OnWindowEvent );
            }
        }

        private void Start ()
        {
            videoRT = GetComponent<VideoPlayer> ().targetTexture;
            if ( !videoRT )
            {
                throw new Exception ( "Not found any RenderTexture on player" );
            }

            volumeCamera = GameObject.FindObjectOfType<VolumeCamera> ();
            if ( volumeCamera )
            {
                volumeCamera.OnWindowEvent.AddListener ( OnWindowEvent );
            }
        }

        private void OnWindowEvent ( VolumeCamera.WindowState state )
        {
            if ( state.IsFocused )
            {
                GetComponent<VideoPlayer> ().Play ();
            }
        }

#if !UNITY_EDITOR
        private void Update ()
        {
            PolySpatialObjectUtils.MarkDirty ( videoRT );
        }
#endif

        private RenderTexture videoRT;

        private VolumeCamera volumeCamera;
    }
}

This issue seems to have been fixed after adding a VideoPlayer.Stop code before playing the video.

Glad you found a workaround!

For the script posted, I would modify OnWindowEvent to be like this:

private void OnWindowEvent ( VolumeCamera.WindowState state )
{
    if (state.WindowEvent == VolumeCamera.WindowEvent.Focused && state.IsFocused)
    {
        GetComponent<VideoPlayer> ().Play ();
    }
}

That way it only triggers it once, specifically on the OnFocus event.