VideoPlayer plays video without Sound

Does the new VideoPlayer support audio playback? The video plays but no sound. I tried to use SetTargetAudioSource but still, no sound. Maybe I am doing something wrong. Anyone have a sample code to play sound with VideoPlayer?

“Audio playback is not ready in this version but will be in a subsequent alpha. However, on OSX and iOS, playing a movie with audio will also play the video if the movie has not been transcoded on import. This functionality will be added to other platforms throughout the alpha and beta cycle.”
– from the Video Playback Preview Document mentioned in the sticky thread Preview of some 5.6 features

Direct mode seems to be busted. However if I wire up an audio source in the editor the audio source mode works fine. I’m currently messing around with trying to get the audio source mode to work at runtime.

I got it working. Look here for how to send the audio to an AudioSource so that you will have complete control of the audio coming from the video.

I saw that post before but the Audio should play in the Editor. It should. That’s not the problem here. If found the solution here.

1 Like

Other variant code for movie in streamingassets folder (must be used on main camera or rawimage) with multilanguage videos.

using System.Collections;
using UnityEngine.Video;
using UnityEngine.UI;
using UnityEngine;

public class Video_Player : MonoBehaviour {
    // Movies Base Folder is Assets/StreamingAssets
    //Video Name To Play [Assign from the Editor with short path]
    // As example: Movies/Intro/Intro.mp4
    public string movie;
    [Range (0f, 1f)]
    // Sound Track volume
    public float soundVolume = 0.5f;
    // Play Video if true
    public bool play;
    private VideoPlayer vPlayer;
    private AudioSource vAudio;
    // Sound Track Number for Play from
    // MultiLang Video
    public ushort audioTrackNum = 0;
    public ushort audioTrackCount = 1;
    private RawImage vSurface;

    void Start (){
        // Select Video & Audio Players
        vPlayer = this.gameObject.AddComponent<VideoPlayer>();       
        vAudio = this.gameObject.AddComponent<AudioSource>();
       
        // Set VideoPlayer Output Mode & Movie Source
        vPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
        vPlayer.source = VideoSource.Url;
       
        // Disable Video & Audio Players play onAwake
        vPlayer.playOnAwake = false;
        vAudio.playOnAwake = false;   

        // Set full Movie Path
        vPlayer.url = "file://"+Application.streamingAssetsPath + "/" + movie;   
        vPlayer.controlledAudioTrackCount = audioTrackCount;
    }
  
    void OnDisable() {
        vPlayer.Stop();
    }
    // Update is called once per frame
    void Update () {
        // Audio Track Enable
        for(int i = 0; i <= audioTrackCount - 1; i++) {
            vPlayer.EnableAudioTrack((ushort)i, false);                   
        }       
        vPlayer.EnableAudioTrack(audioTrackNum, true);       
        vPlayer.SetTargetAudioSource(audioTrackNum, vAudio);

        // Set Vdeo Output to Camera Front Plane
        vAudio.volume = soundVolume;           
        if (vPlayer.gameObject.GetComponent<Camera>() != null)
            vPlayer.renderMode = VideoRenderMode.CameraFrontPlane;       
       
        // Set Vdeo Output to RawImage
        vSurface = vPlayer.gameObject.GetComponent<RawImage>();
        if (vSurface!= null)
            vSurface.texture = vPlayer.texture;       

        // Prepare Video & Play
        if (play) {   
            vPlayer.Prepare();
            vPlayer.Play();
        } else vPlayer.Stop();
    }
}
1 Like

There’s more information on the matter in this thread:

I’m trying to make it so that a pre-rendered FX animation plays on top of an object whenever it’s clicked. I feel like I’m extremely close, but for whatever reason I can’t figure out how to set the Render Mode to Camera Near Plane in this script. It works if I change it in the component while in game play mode, but I need it to start out in Camera near mode. I’ve tried some variations of these in void Start:

vPlayer.renderMode = UnityEngine.Video.VideoTarget.CameraFrontPlane;
vPlayer.target = UnityEngine.Video.VideoTarget.CameraFrontPlane;

But, Unity5.6 doesn’t seem to like VideoTarget or any form of target. Any help would be appreciated as I’m still pretty new to c#.

using System.Collections;
using UnityEngine.Video;
using UnityEngine.UI;
using UnityEngine;
using UnityEditor;

public class PlayVideoNew : MonoBehaviour
{
    public Animator anim;
    private UnityEngine.Video.VideoPlayer vPlayer;
void Start ()
{
    anim = GetComponent<Animator>();
    vPlayer = gameObject.AddComponent<UnityEngine.Video.VideoPlayer>();
    vPlayer.targetCameraAlpha = 0.9F;
    vPlayer.clip = AssetDatabase.LoadAssetAtPath<UnityEngine.Video.VideoClip>("Assets/Videos/ExplosionMovieSTRT.mov");
    vPlayer.Stop();
}
void Update()
{         
    if(Input.GetMouseButtonDown (0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            Debug.Log(Input.mousePosition);

            if (Physics.Raycast(ray, out hit))
                if (hit.collider.tag == "Avatar1")
   
                if (vPlayer.isPlaying)
                {
                    vPlayer.Prepare();
                    vPlayer.Play();
                }
                else
                {
                    anim.Play("CameraShake", -1, 0f);
                    vPlayer.Stop();
                    vPlayer.Play();
                }
        }
    }
}

Hi Benjamin!

The enum values have been renamed for better coherence with camera terminology, so it’s now VideoRenderMode.CameraNearPlane. Thanks for pointing this out, I believe the code example in the API doc is out of date. And you also have to specify which camera you will be targeting. The following should get you going:

vPlayer.renderMode = UnityEngine.Video.VideoRenderMode.CameraNearPlane;
vPlayer.targetCamera = GameObject.Find("Main Camera").GetComponent<Camera>();

Note that if you were adding the VideoPlayer component on the camera object directly, you wouldn’t need to do bind to a camera explicitly, it would be done automatically. But it still would choose the far plane and not the near plane so this part you’d still have to do yourself.

Hope this helps,

Dominique
A/V developer at Unity