Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TacoMakerMan · Dec 15, 2017 at 02:53 PM · errorvideoexceptiontry-catch

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)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JedBeryll · Dec 16, 2017 at 08:53 AM 0
Share

$$anonymous$$aybe it's just a Debug.LogError and not an actual exception.

2 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TacoMakerMan · Feb 16, 2018 at 03:22 PM 0
Share

Thanks, I found a kind of makeshift solution that works already, but this would definitely be the better way of doing it

avatar image Ruhan-_- TacoMakerMan · Feb 16, 2018 at 06:27 PM 0
Share

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

avatar image ookk47oo · Apr 03, 2019 at 08:18 AM 0
Share

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?

avatar image thejox · Jan 19, 2021 at 10:21 AM 0
Share

How do you "avoid memory leaks" by unregistering the error handler only when an error occurs? This strategy rather facilitates a memory leak!

avatar image
0

Answer by KittenSnipes · Dec 15, 2017 at 04:27 PM

@TacoMakerMan

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");
         }
     }

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TacoMakerMan · Dec 16, 2017 at 05:20 AM 0
Share

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.

avatar image KittenSnipes · Dec 16, 2017 at 08:35 AM 0
Share

Is this what you are using:

VideoPlayer Reference

avatar image TacoMakerMan KittenSnipes · Dec 16, 2017 at 05:33 PM 0
Share

Yeah that's right. I can send you more of my script as well if that's necessary.

avatar image KittenSnipes · Dec 17, 2017 at 12:11 PM 0
Share

@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!);
 }

avatar image KittenSnipes · Dec 17, 2017 at 12:12 PM 0
Share

@Taco$$anonymous$$aker$$anonymous$$an

Yeah it would be nice to have more of the script to properly understand what is going on

avatar image TacoMakerMan KittenSnipes · Dec 19, 2017 at 03:04 PM 0
Share

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;
     }
avatar image KittenSnipes TacoMakerMan · Dec 19, 2017 at 03:35 PM 0
Share

@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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

102 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Failed to import assembly System.Runtime.Serialization 0 Answers

Catch Exception from a failed Unity Command 0 Answers

How do I catch the SendMessage Has No Reciever exception thrown when I try to run a function with SendMessage that does not exist? 1 Answer

Exception: INTERNAL configuration error: failed to get configuration 'system.diagnostics' 1 Answer

Need to serialize a Monobehaviour Class 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges