Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Is it possible to switch audio tracks in a video?

Discussion in 'Editor & General Support' started by Franconstein, Jul 3, 2014.

  1. Franconstein

    Franconstein

    Joined:
    Dec 31, 2013
    Posts:
    5
    Note: I posted this question before, in the Animation section, but got no response. Sorry for re-posting.

    Hello! First off, let me say that I'm very new to Unity, so please bare that in mind. I have watched the tutorials, read a great deal of documentation but yet have no actual experience. That said, I am currently working on a project that requires the playback of a video clip. BUT - and of course there's a but - the video hast to be played with up to 5 audio tracks (in order to avoid creating 5 videos, one with an audio track each).

    Now, if I were to solve this problem in any other environment, I'd either use a video file with multiple audio tracks embedded, or just play an audio-less video and an audio file separately. However, I do not know if either is possible with Unity.

    So, my questions are:

    1) Does Unity support video files with multiple audio tracks?

    2) If so, is there a way to switch between audio tracks through code?

    3) If not, is it possible to play back a video and an audio asset simultaneously?

    I am using Unity 4.5.1f3, if that helps.

    Thanks in advance.

    Cheers!
     
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    1. No.
    2. No.
    3. Yes.

    Just to be sure you understand, video is decoded in Unity by software running on the CPU. The decoded frames are then uploaded to the GPU as textures. (Which is why the feature is called MovieTexture.) You should expect the performance to be significantly less that you get from a movie player application which uses the GPU to decode the movie.
     
  3. Franconstein

    Franconstein

    Joined:
    Dec 31, 2013
    Posts:
    5
    Hi! Thanks very much for the info! Sorry it took me so long to be grateful!

    Just another quick question, then (still trying to work around this issue): Is there a way to play the audio track between two set times? Like, start playing at certain time and stop after some other time elapsed?
     
    GarthSmith likes this.
  4. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
  5. Franconstein

    Franconstein

    Joined:
    Dec 31, 2013
    Posts:
    5
    Once again, thank you very much! You rock!

    Cheers!
     
  6. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Well, technically it's the docs that rock.
     
    GarthSmith likes this.
  7. binoman

    binoman

    Joined:
    Sep 29, 2016
    Posts:
    13
    On Unity 2020.1 (and probably some versions before 2020) you can actually use multiple audio tracks of a video. I tried it with an H264 video and ACC audio within a mp4 container. You can actually disable, mute and change the volume of every audio track.
    upload_2020-11-19_10-11-13.png

    So the only question left is, can you change it on runtime using scripts?
     
  8. binoman

    binoman

    Joined:
    Sep 29, 2016
    Posts:
    13
    I just tried it, and it worked perfectly. I used the method
    Code (CSharp):
    1. videoPlayerComponent.SetDirectAudioMute(ushort trackIndex, bool mute)
    .
    I muted all other tracks but the track I need.
    If you want to load a video via URL, you should set the count of the tracks in
    Code (CSharp):
    1. videoPlayerComponent.controlledAudioTrackCount = theCountOfTheTracksYouHave
    ,
    or change the value from the inspector:
    upload_2020-11-19_10-58-5.png

    It was tested using RenderMode: Camera Far Plane on the Unity Editor, but it's not working on WebGL. It needs to be tested on different platforms.
     
    Last edited: Nov 19, 2020