Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Video Video Player is not playing audio

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

  1. OctoSharko

    OctoSharko

    Joined:
    Dec 9, 2016
    Posts:
    34
    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. bd87

    bd87

    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:
    67
    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.
     
  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. GoBackFetchIt

    GoBackFetchIt

    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();
    }
    }
    }
     
    jayconsystems likes this.
  8. plmx

    plmx

    Joined:
    Sep 10, 2015
    Posts:
    308
    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.
     
  9. spark-man

    spark-man

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

    OwenG

    Joined:
    Sep 30, 2013
    Posts:
    20
    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:
    20
    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:
    103
    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:
    3
    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.
     
    fjc likes this.
  14. r618

    r618

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

    iffatmai

    Joined:
    Feb 1, 2018
    Posts:
    1
    I exported an app and the video plays, but no sound was coming out on Android phone.
    I found this video,

    and thought the guys was crazy, but I tried it anyway, sure enough, the sound was playing in the headphone, and by messing with the volume, with and without the headphone, it actually fixed the sound problem!!! go figure
     
  16. rmon222

    rmon222

    Joined:
    Oct 24, 2018
    Posts:
    77
    In case anyone else reads this thread, one year later, all this code isn't necessary (anymore?).
    Just simply...
    videoPlayer.source = VideoSource.Url;
    videoPlayer.url = "file://" + Application.streamingAssetsPath + "/videoName";
    videoPlayer.Play();
    ...will work with 2019.3.0f4
     
  17. paresh_unity496

    paresh_unity496

    Joined:
    May 6, 2019
    Posts:
    5
    WORK FOR ME (0_0)
     
  18. Jon_Olive

    Jon_Olive

    Joined:
    Sep 26, 2017
    Posts:
    23
    Something to be careful of here - the VideoPlayer is quite fussy it seems about the implementation of the audio tracks - I I was still using Quicktime7 Pro to prepare the video - and the VideoPlayer would stubbornly refused to play anything but the first audio track - although recognising the other tracks were there. In fact IIRC it was playing all the tracks - but all mixed down and playing as the first track. It turned out that prepping the video in DaVinci Resolve sorted the problem. Haven't gotten to the bottom of exactly what was going on yet - but worth being aware of.
     
  19. jayconsystems

    jayconsystems

    Joined:
    May 14, 2019
    Posts:
    4
     
  20. LT23Live

    LT23Live

    Joined:
    Jul 8, 2014
    Posts:
    85
    Years later and it still wont play the audio in Direct mode :D
     
  21. srgochman

    srgochman

    Joined:
    Nov 13, 2019
    Posts:
    2
    For any noobs out there like me, make sure that Mute Audio is not highlighted when in Game Mode. :rolleyes:
     

    Attached Files:

    Romaleks360 and lionelIndigo like this.
  22. ABerlemont

    ABerlemont

    Joined:
    Sep 27, 2012
    Posts:
    67
    I'm having the same problem.
    Followed and tried everything in this thread.

    Code (CSharp):
    1.  
    2.     targetAudioSource.playOnAwake = false;
    3.     vp.playOnAwake = false;
    4.  
    5.     vp.targetCameraAlpha = 1.0f;
    6.     vp.isLooping = false;
    7.  
    8.     Debug.Log(getStamp() + " setuping audio ...");
    9.  
    10.     //https://stackoverflow.com/questions/45532234/unity-videoplayer-is-not-playing-audio
    11.     //https://stackoverflow.com/questions/41144054/using-new-unity-videoplayer-and-videoclip-api-to-play-video
    12.     vp.audioOutputMode = VideoAudioOutputMode.AudioSource;
    13.     vp.EnableAudioTrack(0, true);
    14.     vp.SetTargetAudioSource(0, targetAudioSource);
    15.  
    16.     vp.controlledAudioTrackCount = 1;
    17.     targetAudioSource.volume = 1f;
    18.  
    19.     vp.url = solvedClipPath; // path is ok, video is showing
    20.  
    21.     vp.Prepare();
    22.     while (!vp.isPrepared) yield return null;
    23.  
    24.     vp.Play();
    25.     targetAudioSource.Play();
    26.  
    even with callbacks

    Code (CSharp):
    1.  
    2.     (...)
    3.     bool wait = true;
    4.     vp.prepareCompleted += (VideoPlayer vPlayer) =>
    5.     {
    6.       Debug.Log("PREPARE COMPLETED");
    7.       wait = false;
    8.     };
    9.     while (wait) yield return null;
    10.     (...)
    11.  
    Did I miss something ?
     
  23. holmessalex

    holmessalex

    Joined:
    Jan 12, 2021
    Posts:
    1
    Last few weeks, I am also facing a problem like this, It plays video perfectly but no sound while playing it. Now, where can I get access or permission of a video player embedded script to add values? Already shared.
     
  24. VirtualPierogi

    VirtualPierogi

    Joined:
    Sep 3, 2012
    Posts:
    54
    while using webgl im only getting audio and no video, the change was sudden and i dont know what caused it, video is in webm format and firefox throws an error saying that there is no codecs for webm and that is a lie. mp4 has the same issues.
     
  25. Bimbuh

    Bimbuh

    Joined:
    Dec 21, 2018
    Posts:
    12
    T
    Thanks a huge lot mate, works just as I want it
     
    plmx likes this.
  26. atakanhr

    atakanhr

    Joined:
    Jan 7, 2020
    Posts:
    1
    I found a thing about video player. I try all the answers to solve my problem. Not work. After that I convert my video MOV to MP4 . My problem solved. In android there is a problem about MOV Codec. Video is playing but audio is not playing .
    I am making vr project to Oculus Quest. When i use MOV codec , Audio play in editor, and connect glass with openxr. But not in build. This solved my problem.