Video Player is not playing audio

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();
audioSource = gameObject.AddComponent();
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

nice list

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.

5 Likes

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.

6 Likes

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.

I second Vizualfly’s question? It seem the new update has hidden the script making it impossible to get the audio to play.

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();
audioPlayer = gameObject.AddComponent();
// 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();
}
}
}

1 Like

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.

// We want to play from a URL.
// Set the source mode FIRST, before changing audio settings;
// as setting the source mode will reset those.
videoPlayer.source = VideoSource.Url;

// Set mode to Audio Source.
videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;

// We want to control one audio track with the video player
videoPlayer.controlledAudioTrackCount = 1;

// We enable the first track, which has the id zero
videoPlayer.EnableAudioTrack(0, true);

// ...and we set the audio source for this track
videoPlayer.SetTargetAudioSource(0, audioSource);

// now set an url to play
videoPlayer.url = "...some url..."

Aftwards, in a coroutine, prepare and play:

videoPlayer.Prepare();

while (!videoPlayer.isPrepared) {
    yield return new WaitForEndOfFrame();
}

videoPlayer.Play();

HTH.

7 Likes

Perfect! Tested on android. Audio is working along with video.

2 Likes

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:

public void Awake()
{
    videoPlayer = videoGO.AddComponent<VideoPlayer>();
    videoAudioSource = videoGO.AddComponent<AudioSource>();
    videoAudioSource.playOnAwake = false;

    videoPlayer.source = VideoSource.Url;
    videoPlayer.url = Application.streamingAssetsPath + Path.DirectorySeparatorChar + movieFileName;

    videoPlayer.playOnAwake = false;
    videoPlayer.isLooping = true;

    videoPlayer.renderMode = VideoRenderMode.RenderTexture;
    videoPlayer.targetTexture = videoRenderTexture;
    videoPlayer.aspectRatio = VideoAspectRatio.FitOutside;

    videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
    videoPlayer.controlledAudioTrackCount = 1;
    videoPlayer.EnableAudioTrack(0, true);
    videoPlayer.SetTargetAudioSource(0, videoAudioSource);
    videoAudioSource.volume = 1f;

    StartCoroutine(PrepareAndPlayVideo());
}

IEnumerator PrepareAndPlayVideo()
{
    videoPlayer.Prepare();

    while (!videoPlayer.isPrepared)
    {
        Debug.Log("Preparing Video");
        yield return null;
    }

    Debug.Log("Done prepping.");

    videoPlayer.Play();
    videoAudioSource.Play();
}

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? :slight_smile:

Thanks!
Owen

For anyone coming along later, I found a solution that’s good enough for me. :slight_smile: 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. :slight_smile:

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.

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:

public class VideoTest : MonoBehaviour {

    public VideoPlayer videoPlayer;
    public AudioSource audioPlayer;
    void OnEnable()
    {
        // Will attach a VideoPlayer to the main camera.
        GameObject camera = GameObject.Find("Main 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.audioOutputMode = VideoAudioOutputMode.AudioSource;
        videoPlayer.EnableAudioTrack(0, true);
        videoPlayer.SetTargetAudioSource(0, audioPlayer);
        videoPlayer.controlledAudioTrackCount = 1;
        audioPlayer.volume = 1.0f;
        //audioPlayer.controlledAudioTrackCount = 1;

#if UNITY_EDITOR
        videoPlayer.url = "file:///Users/shad/Desktop/recording_2018_10_13_10_56_54_606.mp4";
#else
        videoPlayer.url = Application.streamingAssetsPath + "/recording_2018_10_13_10_56_54_606.mp4";
#endif


    }
    public void PlayVid()
    {

        if (videoPlayer.isPlaying)
        {
            videoPlayer.Pause();
            audioPlayer.Pause();
        }
        else if (Input.GetKeyDown("space"))
        {
            videoPlayer.Stop();
        }
        else
        {
            StartCoroutine(PlayAfterPrepare());
        }
    }

    IEnumerator PlayAfterPrepare()
    {
        videoPlayer.Prepare();

        while (!videoPlayer.isPrepared)
        {
            yield return new WaitForEndOfFrame();
        }

        videoPlayer.Play();
        audioPlayer.Play();
    }

    private void Update()
    {
        if (Input.anyKeyDown) {
            PlayVid();
        }
        else if (Input.touchSupported && Input.touchCount > 0) {
            PlayVid();
        }
    }

}

Any help getting this working would be greatly appreciated.

1 Like

try replacing

yield return new WaitForEndOfFrame();

with

yield return null;

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

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

WORK FOR ME (0_0)

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.

Years later and it still wont play the audio in Direct mode :smile: