- Home /
Try Catch not working with VideoPlayer
I am trying to play a video using Unity's VideoPlayer which I have attached to the camera. I want the user to be able to enter the file path of a local video file and the VideoPlayer should then play the video. I got this working fine but I can't seem to handle exceptions when the user enters a path that is not valid, for instance:
try
{
videoPlayer.url = @"C:\blah";
}
catch
{
print ("error");
}
does not print "error" but instead continues execution and gives me an error in the console saying "Can't play movie [C:\blah]"
I can't think of any reason why this error shouldn't be caught by using the try catch method. I am using the latest version of Unity. If you cannot use the try catch method to catch this exception, then I need a way to test if the url is valid and will play properly (e.g. the file is not corrupt)
$$anonymous$$aybe it's just a Debug.LogError and not an actual exception.
Answer by Ruhan-_- · Feb 16, 2018 at 03:11 AM
@TacoMakerMan You just need to register to the built in errorReceived event of the video player. [In case the answer wasn't found and for anyone else searching]
private void Start () {
loadingUI.GetComponentInChildren<Text>().text = "Loading Video\nPlease Wait";
videoPlayer.url = "My URL";
videoPlayer.errorReceived += VideoPlayer_errorReceived;//THIS LINE!!!
videoPlayer.Prepare();
}
private void VideoPlayer_errorReceived (VideoPlayer source, string message) {
loadingUI.GetComponentInChildren<Text>().text = "This video is incompatible.\nThe video should be encoded\nwith H.264 as an MP4 file.";
videoPlayer.errorReceived -= VideoPlayer_errorReceived;//Unregister to avoid memory leaks
}
Thanks, I found a kind of makeshift solution that works already, but this would definitely be the better way of doing it
No problem mate, happy to help! You'll notice if you use your VideoPlayer component with dot "." you can view it's inner stuff The events are at the very end videoPlayer. <--view its events with intellisense, that's how I always try to find something whether it exists or not I know you already know it :P but simple stuff can be overlooked easily It has many useful events, like seekCompleted and loopPointReached which would otherwise need makeshift ways to do manually
Hi there.I register to this event,but it still prints error messages on the console. Is there any way to avoid these error messages?
How do you "avoid memory leaks" by unregistering the error handler only when an error occurs? This strategy rather facilitates a memory leak!
Answer by KittenSnipes · Dec 15, 2017 at 04:27 PM
Here is what I do for files so I hope it helps:
//The desired directory name of the folder that will be created in your drive
public string directoryName = "PlayerFiles";
//Path name of the file that will be in your directory
public string pathName = "playerData.txt";
//Private variables used to give the full names of their locations
string fullDirectoryName;
string fullPathName;
void Start () {
//Remember the double slashes are important in path names
//This is the full location of the directory
fullDirectoryName = "C://" + directoryName;
//This is the full location of the path/file
fullPathName = fullDirectoryName + "//" + fullPathName;
//Checks if the directory exists and if it does not then it will create it
if (!(Directory.Exists(fullDirectoryName)))
{
Directory.CreateDirectory("C://" + directoryName);
}
//Checks if the file exists and if not then it returns
if (!(File.Exists(fullPathName)))
{
Debug.Log("Video File Does Not Exist!");
return;
}
//This is the best case scenario. The file exists and will play
else
{
Debug.Log("The video is playing");
}
}
Thanks for the reply, but although this script should make sure that the file exists, it will not make sure it will play. For instance, some mp4 files, although they exist and are not corrupted or anything, will not play because they are not encoded in the correct format for the videoPlayer to play them.
Yeah that's right. I can send you more of my script as well if that's necessary.
@Taco$$anonymous$$aker$$anonymous$$an
Well then I guess all I can think of is using this:
if (!(videoPlayer.isPlaying)) {
Debug.Log(Error: Video Invalid);
}
else {
Debug.Log(“Yay it worked!);
}
@Taco$$anonymous$$aker$$anonymous$$an
Yeah it would be nice to have more of the script to properly understand what is going on
Unfortunately the code you posted wouldn't work because if the video is invalid it won't change the url so the last video will just keep playing so isPlaying will still be true. It turns out the reason that the try-catch isn't catching the exception is because I don't think it's actually an exception but rather just an error message like a Debug.LogError or something.
I still don't know what to do now though, I'll post more of my code:
IEnumerator _PlayVideo (string url)
{
var prevUrl = background_video.url;
videoUrl = backgroundVideo.text;
background_video.enabled = true;
cameraAudioSource.enabled = true;
print (TrySetUrl (videoUrl));
if (!TrySetUrl (videoUrl))
yield break;
background_video.Prepare ();
videoPreparingPanel.SetActive (true);
yield return new WaitUntil (() => background_video.isPrepared);
videoPreparingPanel.SetActive (false);
if (TrySetUrl (videoUrl))
{
//Play Video
background_video.Play ();
//Play Sound
cameraAudioSource.Play ();
PlayerPrefs.SetString ("videoUrl", videoUrl);
}
else
{
background_video.url = prevUrl;
videoPlaying = false;
backgroundVideo.enabled = false;
cameraAudioSource.enabled = false;
videoPreparingPanel.SetActive (false);
System.Windows.Forms.$$anonymous$$essageBox.Show ("Error Playing '" + videoUrl + "'");
}
}
bool TrySetUrl (string url)
{
try
{
background_video.url = url;
}
catch
{
return false;
}
return true;
}
@Taco$$anonymous$$aker$$anonymous$$an
Try using : videoPlayer.isPrepared;
This is what it does: Says Whether the VideoPlayer has successfully prepared the content to be played. (Read Only)
Like while trying to prepare the video using the set url return if it works or not.