Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Detect if video has audio track (from URL)

Discussion in 'Audio & Video' started by halinc, Apr 13, 2023.

  1. halinc

    halinc

    Joined:
    Feb 24, 2019
    Posts:
    32
    I'm loading different videos from URL and play them in the Unity VideoPlayer component. Is there any way to detect whether the video has an audio track (or multiple) or not?

    Use case: would like to show audio icon to user only if the video has audio.

    I tried .audioTrackCount and .controlledAudioTrackCount. audioTrackCount always returns 0, controlledAudioTrackCount always returns 1. I wait till isPrepared = true before I check the counts.

    Also if I check the VideoPlayer component in the Inspector Controlled Tracks shows 1 - even for videos without any audio tracks.
     
  2. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    438
    What you're suggesting there seem the be the good... Could you share some of your code? Thanks!
     
  3. halinc

    halinc

    Joined:
    Feb 24, 2019
    Posts:
    32
    sure, thanks for your reply. here is the basic code I'm using:

    Code (CSharp):
    1. public async UniTask<bool> LoadVideo(VideoPlayer videoPlayer, string url)
    2.     {
    3.         bool prepareSuccess = await PrepareVideo(videoPlayer, url, aRExample);
    4.         if (!prepareSuccess || videoPlayer == null || videoPlayer.gameObject == null)
    5.             {
    6.                 return false;
    7.             }
    8.         videoPlayer.gameObject.SetActive(true);
    9.  
    10.         // this is not working, controlledAudioTrackCount always 1, audioTrackCount always 0
    11.         if (videoPlayer.controlledAudioTrackCount > 0 )
    12.         {
    13.             Debug.Log("should show audio hint for video!");
    14.         }
    15.  
    16.     }
    17.  
    18. private async UniTask<bool> PrepareVideo(VideoPlayer videoPlayer, string url)
    19.     {
    20.         try
    21.         {
    22.             // Wait until video is prepared
    23.             videoPlayer.url = url;
    24.             videoPlayer.gameObject.transform.localScale = new Vector3(0f, 0f, 0f);
    25.             videoPlayer.gameObject.SetActive(true);
    26.             videoPlayer.errorReceived += VideoPlayer_errorReceived;
    27.             videoPlayer.Prepare();
    28.             _cancellationTokenSource = new CancellationTokenSource();
    29.             await UniTask.WaitUntil((() => videoPlayer == null || videoPlayer.isPrepared),
    30.                 cancellationToken: _cancellationTokenSource.Token);
    31.             videoPlayer.aspectRatio = VideoAspectRatio.FitHorizontally;
    32.             return true;
    33.         }
    34.         catch (Exception e)
    35.         {
    36.             Debug.Log("catch error: " + e);
    37.             if (videoPlayer != null && videoPlayer.gameObject != null)
    38.             {
    39.                 videoPlayer.gameObject.SetActive(false);
    40.             }
    41.             return false;
    42.         }
    43.     }
     
  4. SeventhString

    SeventhString

    Unity Technologies

    Joined:
    Jan 12, 2023
    Posts:
    438
    I'm generally more the audio guy, but I gather that sometimes metadata is not completely available just after Prepare(). You might have to start the video for everything to be set right. Do you still get this if you hook your check to the videoPlayer.started callback?

    @The_Island any thought?
     
  5. halinc

    halinc

    Joined:
    Feb 24, 2019
    Posts:
    32
    Perfect, in the videoPlayer.started callback it works if I check for videoPlayer.audioTrackCount. Thank you very much for your help!
     
    SeventhString likes this.