Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Let us know a bit about your interests, and if you'd like to become more directly involved. Take our survey!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Video Video Player is not playing audio

Discussion in 'Audio & Video' started by LeRosbif, Aug 6, 2017.

  1. LeRosbif

    LeRosbif

    Joined:
    Dec 9, 2016
    Posts:
    9
    I am struggling to get the sounds out the video from Unity Video Player.

    The video is playing fine but the sound is not working.

    Here is the code I use based on this thread.

    public VideoClip VideoClip;
    private AudioSource audioSource;
    private IEnumerator videoCoRoutine;

    void Awake()
    {
    VideoPlayer videoPlayer = gameObject.AddComponent<VideoPlayer>();
    audioSource = gameObject.AddComponent<AudioSource>();
    videoPlayer.clip = VideoClip;
    videoPlayer.source = VideoSource.VideoClip;
    //videoPlayer.Prepare();
    videoPlayer.renderMode = VideoRenderMode.CameraNearPlane;
    videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
    videoPlayer.EnableAudioTrack(0, true);
    videoPlayer.SetTargetAudioSource(0, audioSource);
    videoPlayer.Play();
    audioSource.Play();
    //StartCoroutine(PlayVid());
    ContinueBtn.onClick.AddListener(OnContinue);
    }

    Can anyone kindly help ?

    Thanks
     
  2. JacobVR

    JacobVR

    Joined:
    Feb 2, 2016
    Posts:
    14
    nice list
     
  3. jstokes

    jstokes

    Joined:
    Apr 29, 2013
    Posts:
    2
    God this place is useless.

    I solved this crap myself. Unity needs better documentation on USAGE not specification, specification is great.

    @OP
    comment the line for audioSource.Play(). If you trigger video and audio it fails to play audio.
    The problem is that we are all referencing source code from examples that are dated and no longer 100% valid.

    The only other difference to your code from mine, where mine is working, and outside the mentioned change, is:
    audioSource.volume=1.0f;
    audioSource.controlledAudioTrackCount=1;
    prior to the Prepare() statement (actually you don't have that either).

    Use the callbacks...
    vPlayer.loopPointReached and vPlayer.prepareCompleted.

    I finally have it working, 8 hours later.
     
  4. stefanob

    stefanob

    Joined:
    Nov 26, 2012
    Posts:
    32
    I also noticed the Controlled AudioTracks List was empty when the video started playing. I had to set "videoPlayer.controlledAudioTrackCount = 1;" to make it work.
     
    UrsanonVarunit and ireth_86 like this.
  5. vizualfly

    vizualfly

    Joined:
    Jan 15, 2018
    Posts:
    2
    I'm currently having the same problem. Video plays find in texture however no sound at all. Where can I access the video player embedded script to add the values you guys listed? I can't find access to it (script) in inspector.

    Thanks ahead of time.
     
  6. charitythesheha

    charitythesheha

    Joined:
    Jan 30, 2016
    Posts:
    1
    I second Vizualfly's question? It seem the new update has hidden the script making it impossible to get the audio to play.
     
  7. gbaburto

    gbaburto

    Joined:
    Apr 12, 2018
    Posts:
    1
    Just in case anyone is looking for working code. Here is what I have so far:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Video;
    public class PlayVideo1 : MonoBehaviour
    {
    public VideoPlayer videoPlayer;
    public AudioSource audioPlayer;
    void OnEnable()
    {
    // Will attach a VideoPlayer to the main camera.
    GameObject camera = GameObject.Find("Camera");
    // VideoPlayer automatically targets the camera backplane when it is added
    // to a camera object, no need to change videoPlayer.targetCamera.
    videoPlayer = camera.AddComponent<VideoPlayer>();
    audioPlayer = gameObject.AddComponent<AudioSource>();
    // Play on awake defaults to true. Set it to false to avoid the url set
    // below to auto-start playback since we're in Start(). // Changed to "onEnable"
    videoPlayer.playOnAwake = false;
    audioPlayer.playOnAwake = false;
    // By default, VideoPlayers added to a camera will use the far plane.
    // Let's target the near plane instead.
    videoPlayer.renderMode = VideoRenderMode.CameraNearPlane;
    // This will cause our scene to be visible through the video being played. // Changed it to 1.0 for it not to be transparent
    videoPlayer.targetCameraAlpha = 1.0F;
    // Restart from beginning when done. // Do not restart when done playing
    videoPlayer.isLooping = false;
    // Set the video to play. URL supports local absolute or relative paths.
    // Here, using absolute.
    videoPlayer.url = "C:/Users/user/Documents/vid.mp4";
    videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
    videoPlayer.EnableAudioTrack(0, true);
    videoPlayer.SetTargetAudioSource(0, audioPlayer);
    videoPlayer.controlledAudioTrackCount = 1;
    audioPlayer.volume = 1.0f;
    //audioPlayer.controlledAudioTrackCount = 1;
    }
    public void PlayVid ()
    {

    if (videoPlayer.isPlaying)
    {
    videoPlayer.Pause();
    audioPlayer.Pause();
    }
    //else if (Input.GetKeyDown("space"))
    //{
    // videoPlayer.Stop();
    //}
    else {
    videoPlayer.Play();
    audioPlayer.Play();
    }
    }
    }
     
  8. plmx

    plmx

    Joined:
    Sep 10, 2015
    Posts:
    192
    Audio settings seem to work differently when playing from a clip and from a URL. For playing from a URL, in 2018.1.2f1, the correct sequence to set up the player is this. First, set up the player; player must be stopped for this, and not been Prepare()d yet.

    Code (CSharp):
    1. // We want to play from a URL.
    2. // Set the source mode FIRST, before changing audio settings;
    3. // as setting the source mode will reset those.
    4. videoPlayer.source = VideoSource.Url;
    5.  
    6. // Set mode to Audio Source.
    7. videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
    8.  
    9. // We want to control one audio track with the video player
    10. videoPlayer.controlledAudioTrackCount = 1;
    11.  
    12. // We enable the first track, which has the id zero
    13. videoPlayer.EnableAudioTrack(0, true);
    14.  
    15. // ...and we set the audio source for this track
    16. videoPlayer.SetTargetAudioSource(0, audioSource);
    17.  
    18. // now set an url to play
    19. videoPlayer.url = "...some url..."
    Aftwards, in a coroutine, prepare and play:

    Code (CSharp):
    1. videoPlayer.Prepare();
    2.  
    3. while (!videoPlayer.isPrepared) {
    4.     yield return new WaitForEndOfFrame();
    5. }
    6.  
    7. videoPlayer.Play();
    HTH.
     
    Aldo-V likes this.
  9. Aldo-V

    Aldo-V

    Joined:
    May 22, 2013
    Posts:
    66
    Perfect! Tested on android. Audio is working along with video.
     
  10. OwenG

    OwenG

    Joined:
    Sep 30, 2013
    Posts:
    18
    Does anyone have any tips of getting a video to play with audio via a URL (in StreamingAssets) in 2017.4.4? I've been trying all day to get this to work. Here's where I'm at:

    - If I use a VideoClip version of my movie, it works fine
    - If I manually add the VideoPlayer and AudioSource components to my GameObject and use the same settings as in my script, it works fine
    - But, if I create the VideoPlayer and AudioSource via code, the video plays fine, but no audio plays.

    This leads me to believe it might be some kind of timing/initialization thing? But I'm running out of ideas. This is what my code looks like:

    Code (CSharp):
    1. public void Awake()
    2. {
    3.     videoPlayer = videoGO.AddComponent<VideoPlayer>();
    4.     videoAudioSource = videoGO.AddComponent<AudioSource>();
    5.     videoAudioSource.playOnAwake = false;
    6.  
    7.     videoPlayer.source = VideoSource.Url;
    8.     videoPlayer.url = Application.streamingAssetsPath + Path.DirectorySeparatorChar + movieFileName;
    9.  
    10.     videoPlayer.playOnAwake = false;
    11.     videoPlayer.isLooping = true;
    12.  
    13.     videoPlayer.renderMode = VideoRenderMode.RenderTexture;
    14.     videoPlayer.targetTexture = videoRenderTexture;
    15.     videoPlayer.aspectRatio = VideoAspectRatio.FitOutside;
    16.  
    17.     videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
    18.     videoPlayer.controlledAudioTrackCount = 1;
    19.     videoPlayer.EnableAudioTrack(0, true);
    20.     videoPlayer.SetTargetAudioSource(0, videoAudioSource);
    21.     videoAudioSource.volume = 1f;
    22.  
    23.     StartCoroutine(PrepareAndPlayVideo());
    24. }
    25.  
    26. IEnumerator PrepareAndPlayVideo()
    27. {
    28.     videoPlayer.Prepare();
    29.  
    30.     while (!videoPlayer.isPrepared)
    31.     {
    32.         Debug.Log("Preparing Video");
    33.         yield return null;
    34.     }
    35.  
    36.     Debug.Log("Done prepping.");
    37.  
    38.     videoPlayer.Play();
    39.     videoAudioSource.Play();
    40. }
    I have tried it with playOnAwake set to true for both the audio and video. I've tried it without the coroutine and using playOnAwake set to true. I've tried just playing the video without prepping it. But if I run the game with this code and copy the components and paste them back onto my gameobject in the scene, it works with these exact settings.

    I'm kind of at a loss about what to try next. For various reasons it's unfortunately not an option to leave the VideoPlayer component active on the GameObject at all times. Anyone got any ideas? Is this a known issue in 2017.4.4? Or am I doing something dumb? :)

    Thanks!
    Owen
     
  11. OwenG

    OwenG

    Joined:
    Sep 30, 2013
    Posts:
    18
    For anyone coming along later, I found a solution that's good enough for me. :) Instead of creating things via code, I created the VideoPlayer and AudioSource on a GameObject in my scene, then saved that out as a prefab to my Resources folder, then deleted it from the scene. Now I can load and instantiate the prefab at runtime, set the URL on the VideoPlayer component, and it all works. I have no idea why the code-only way doesn't work. Maybe it's a problem in 2017.4.4, or something I was doing wrong, but at least I have working audio and video now. :)
     
  12. Justin-Wasilenko

    Justin-Wasilenko

    Joined:
    Mar 10, 2015
    Posts:
    72
    Has anyone found a solution to this? I am also having this problem where creating everything by code results in the audio not playing.

    Setting up the components as normal the audio plays like normal. Trying to do this by code, because one of the target platforms doesn't support all the normal video player functions.
     
  13. kenmr

    kenmr

    Joined:
    Jul 19, 2018
    Posts:
    1
    I'm having trouble with this too. Everything works fine in the editor, but when deployed to an Android S8 the video plays without sound. I'm on Unity 2018.2.12f1, and for testing I created a new project that only has one empty object added with the following script:

    Code (CSharp):
    1. public class VideoTest : MonoBehaviour {
    2.  
    3.     public VideoPlayer videoPlayer;
    4.     public AudioSource audioPlayer;
    5.     void OnEnable()
    6.     {
    7.         // Will attach a VideoPlayer to the main camera.
    8.         GameObject camera = GameObject.Find("Main Camera");
    9.         // VideoPlayer automatically targets the camera backplane when it is added
    10.         // to a camera object, no need to change videoPlayer.targetCamera.
    11.         videoPlayer = camera.AddComponent<VideoPlayer>();
    12.         audioPlayer = gameObject.AddComponent<AudioSource>();
    13.         // Play on awake defaults to true. Set it to false to avoid the url set
    14.         // below to auto-start playback since we're in Start(). // Changed to "onEnable"
    15.         videoPlayer.playOnAwake = false;
    16.         audioPlayer.playOnAwake = false;
    17.         // By default, VideoPlayers added to a camera will use the far plane.
    18.         // Let's target the near plane instead.
    19.         videoPlayer.renderMode = VideoRenderMode.CameraNearPlane;
    20.         // This will cause our scene to be visible through the video being played. // Changed it to 1.0 for it not to be transparent
    21.         videoPlayer.targetCameraAlpha = 1.0F;
    22.         // Restart from beginning when done. // Do not restart when done playing
    23.         videoPlayer.isLooping = false;
    24.         // Set the video to play. URL supports local absolute or relative paths.
    25.         // Here, using absolute.
    26.         videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
    27.         videoPlayer.EnableAudioTrack(0, true);
    28.         videoPlayer.SetTargetAudioSource(0, audioPlayer);
    29.         videoPlayer.controlledAudioTrackCount = 1;
    30.         audioPlayer.volume = 1.0f;
    31.         //audioPlayer.controlledAudioTrackCount = 1;
    32.  
    33. #if UNITY_EDITOR
    34.         videoPlayer.url = "file:///Users/shad/Desktop/recording_2018_10_13_10_56_54_606.mp4";
    35. #else
    36.         videoPlayer.url = Application.streamingAssetsPath + "/recording_2018_10_13_10_56_54_606.mp4";
    37. #endif
    38.  
    39.  
    40.     }
    41.     public void PlayVid()
    42.     {
    43.  
    44.         if (videoPlayer.isPlaying)
    45.         {
    46.             videoPlayer.Pause();
    47.             audioPlayer.Pause();
    48.         }
    49.         else if (Input.GetKeyDown("space"))
    50.         {
    51.             videoPlayer.Stop();
    52.         }
    53.         else
    54.         {
    55.             StartCoroutine(PlayAfterPrepare());
    56.         }
    57.     }
    58.  
    59.     IEnumerator PlayAfterPrepare()
    60.     {
    61.         videoPlayer.Prepare();
    62.  
    63.         while (!videoPlayer.isPrepared)
    64.         {
    65.             yield return new WaitForEndOfFrame();
    66.         }
    67.  
    68.         videoPlayer.Play();
    69.         audioPlayer.Play();
    70.     }
    71.  
    72.     private void Update()
    73.     {
    74.         if (Input.anyKeyDown) {
    75.             PlayVid();
    76.         }
    77.         else if (Input.touchSupported && Input.touchCount > 0) {
    78.             PlayVid();
    79.         }
    80.     }
    81.  
    82. }
    Any help getting this working would be greatly appreciated.
     
  14. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    635
    try replacing
    Code (CSharp):
    1. yield return new WaitForEndOfFrame();
    with
    Code (CSharp):
    1. yield return null;