Skip to content

Instantly share code, notes, and snippets.

@junaid109
Created March 28, 2024 11:05
Show Gist options
  • Save junaid109/4d144653a5dc86c202b3128bb90903ad to your computer and use it in GitHub Desktop.
Save junaid109/4d144653a5dc86c202b3128bb90903ad to your computer and use it in GitHub Desktop.
Simple script to play video with avpro
using UnityEngine;
using RenderHeads.Media.AVProVideo;
public class VideoPlayerController : MonoBehaviour
{
public string videoURL = "YOUR_VIDEO_URL_HERE";
public bool playOnStart = true;
private MediaPlayer mediaPlayer;
private void Start()
{
// Get the MediaPlayer component attached to this GameObject
mediaPlayer = GetComponent<MediaPlayer>();
// Subscribe to events
mediaPlayer.Events.AddListener(OnMediaPlayerEvent);
// Load and play the video
if (playOnStart)
{
LoadAndPlayVideo();
}
}
public void LoadAndPlayVideo()
{
// Ensure a valid MediaPlayer component is available
if (mediaPlayer == null)
{
Debug.LogError("MediaPlayer component not found.");
return;
}
// Set the video URL
mediaPlayer.OpenVideoFromFile(MediaPlayer.FileLocation.AbsolutePathOrURL, videoURL);
// Play the video
mediaPlayer.Play();
}
private void OnMediaPlayerEvent(MediaPlayer mp, MediaPlayerEvent.EventType eventType, ErrorCode errorCode)
{
// Handle media player events here if needed
switch (eventType)
{
case MediaPlayerEvent.EventType.FinishedPlaying:
Debug.Log("Video playback finished.");
break;
case MediaPlayerEvent.EventType.Error:
Debug.LogError("Error: " + errorCode.ToString());
break;
default:
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment