Files

76 lines
2.3 KiB
C#
Raw Permalink Normal View History

2026-11-30 19:13:17 +00:00
using UnityEngine;
// Somnium Bridge namespace (per documentation)
using SomniumSpace.Bridge;
public class VideoPlayerController : MonoBehaviour
{
// Reference to the SomniumVideoPlayer component
[SerializeField] private SomniumVideoPlayer _videoPlayer;
// Unity Editor only field for setting the video URL.
// This is exposed in the Inspector so you can set it before publishing,
// but has no runtime UI, ensuring only one master controls it.
[Tooltip("Enter your test video URL here in the Unity Inspector.\nThis value is used at load time and cannot be changed in-game.")]
public string _videoUrl;
// Optional: Slider for volume control if desired
[SerializeField] private Slider _volumeSlider;
private void Start()
{
if (_videoPlayer == null) return;
// Set the video link and prepare it (Unity Editor input only)
if (!string.IsNullOrEmpty(_videoUrl))
{
_videoPlayer.SetLinkAndPrepare(_videoUrl);
}
// Subscribe to video events
_videoPlayer.OnEventReceived.AddListener(OnVideoEventReceived);
}
private void OnDisable()
{
if (_videoPlayer != null)
{
_videoPlayer.OnEventReceived.RemoveListener(OnVideoEventReceived);
}
}
private void Update()
{
// Keep slider in sync with player volume if assigned
if (_volumeSlider != null && _videoPlayer != null)
{
_volumeSlider.value = _videoPlayer.GetVolume();
}
}
public void PlayVideo() => _videoPlayer?.Play();
public void PauseVideo() => _videoPlayer?.Pause();
public void StopVideo() => _videoPlayer?.Stop();
// Handle events based on Somnium Video Events (ReadyToPlay, Started, etc.)
private void OnVideoEventReceived(SomniumVideoEvent videoEvent)
{
switch (videoEvent)
{
case SomniumVideoEvent.ReadyToPlay:
Debug.Log("Video Ready to Play");
break;
case SomniumVideoEvent.Started:
Debug.Log("Video Started");
break;
case SomniumVideoEvent.FinishedPlaying:
Debug.Log("Video Finished - Pausing automatically");
_videoPlayer?.Pause();
break;
default:
break;
}
}
}