Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

VideoPlayer plays video without Sound

Discussion in '5.6 Beta' started by hangemhigh, Dec 14, 2016.

  1. hangemhigh

    hangemhigh

    Joined:
    Aug 2, 2014
    Posts:
    56
    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?
     
  2. zyzyx

    zyzyx

    Joined:
    Jul 9, 2012
    Posts:
    226
    "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
     
  3. darthbator

    darthbator

    Joined:
    Jan 21, 2012
    Posts:
    169
    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.
     
  4. hangemhigh

    hangemhigh

    Joined:
    Aug 2, 2014
    Posts:
    56
    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.
     
  5. hangemhigh

    hangemhigh

    Joined:
    Aug 2, 2014
    Posts:
    56
    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.
     
    zyzyx likes this.
  6. Gray_Master

    Gray_Master

    Joined:
    Jun 20, 2010
    Posts:
    152
    Other variant code for movie in streamingassets folder (must be used on main camera or rawimage) with multilanguage videos.
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine.Video;
    3. using UnityEngine.UI;
    4. using UnityEngine;
    5.  
    6. public class Video_Player : MonoBehaviour {
    7.     // Movies Base Folder is Assets/StreamingAssets
    8.     //Video Name To Play [Assign from the Editor with short path]
    9.     // As example: Movies/Intro/Intro.mp4
    10.     public string movie;
    11.     [Range (0f, 1f)]
    12.     // Sound Track volume
    13.     public float soundVolume = 0.5f;
    14.     // Play Video if true
    15.     public bool play;
    16.     private VideoPlayer vPlayer;
    17.     private AudioSource vAudio;
    18.     // Sound Track Number for Play from
    19.     // MultiLang Video
    20.     public ushort audioTrackNum = 0;
    21.     public ushort audioTrackCount = 1;
    22.     private RawImage vSurface;
    23.  
    24.     void Start (){
    25.         // Select Video & Audio Players
    26.         vPlayer = this.gameObject.AddComponent<VideoPlayer>();      
    27.         vAudio = this.gameObject.AddComponent<AudioSource>();
    28.        
    29.         // Set VideoPlayer Output Mode & Movie Source
    30.         vPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;
    31.         vPlayer.source = VideoSource.Url;
    32.        
    33.         // Disable Video & Audio Players play onAwake
    34.         vPlayer.playOnAwake = false;
    35.         vAudio.playOnAwake = false;  
    36.  
    37.         // Set full Movie Path
    38.         vPlayer.url = "file://"+Application.streamingAssetsPath + "/" + movie;  
    39.         vPlayer.controlledAudioTrackCount = audioTrackCount;
    40.     }
    41.  
    42.     void OnDisable() {
    43.         vPlayer.Stop();
    44.     }
    45.     // Update is called once per frame
    46.     void Update () {
    47.         // Audio Track Enable
    48.         for(int i = 0; i <= audioTrackCount - 1; i++) {
    49.             vPlayer.EnableAudioTrack((ushort)i, false);                  
    50.         }      
    51.         vPlayer.EnableAudioTrack(audioTrackNum, true);      
    52.         vPlayer.SetTargetAudioSource(audioTrackNum, vAudio);
    53.  
    54.         // Set Vdeo Output to Camera Front Plane
    55.         vAudio.volume = soundVolume;          
    56.         if (vPlayer.gameObject.GetComponent<Camera>() != null)
    57.             vPlayer.renderMode = VideoRenderMode.CameraFrontPlane;      
    58.        
    59.         // Set Vdeo Output to RawImage
    60.         vSurface = vPlayer.gameObject.GetComponent<RawImage>();
    61.         if (vSurface!= null)
    62.             vSurface.texture = vPlayer.texture;      
    63.  
    64.         // Prepare Video & Play
    65.         if (play) {  
    66.             vPlayer.Prepare();
    67.             vPlayer.Play();
    68.         } else vPlayer.Stop();
    69.     }
    70. }
    71.  
     
    ercion likes this.
  7. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,132
  8. BenjaminMarkus

    BenjaminMarkus

    Joined:
    Feb 7, 2017
    Posts:
    2
    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#.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine.Video;
    3. using UnityEngine.UI;
    4. using UnityEngine;
    5. using UnityEditor;
    6.  
    7. public class PlayVideoNew : MonoBehaviour
    8. {
    9.     public Animator anim;
    10.     private UnityEngine.Video.VideoPlayer vPlayer;
    11. void Start ()
    12. {
    13.     anim = GetComponent<Animator>();
    14.     vPlayer = gameObject.AddComponent<UnityEngine.Video.VideoPlayer>();
    15.     vPlayer.targetCameraAlpha = 0.9F;
    16.     vPlayer.clip = AssetDatabase.LoadAssetAtPath<UnityEngine.Video.VideoClip>("Assets/Videos/ExplosionMovieSTRT.mov");
    17.     vPlayer.Stop();
    18. }
    19. void Update()
    20. {        
    21.     if(Input.GetMouseButtonDown (0))
    22.         {
    23.             RaycastHit hit;
    24.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    25.             Debug.Log(Input.mousePosition);
    26.  
    27.             if (Physics.Raycast(ray, out hit))
    28.                 if (hit.collider.tag == "Avatar1")
    29.    
    30.                 if (vPlayer.isPlaying)
    31.                 {
    32.                     vPlayer.Prepare();
    33.                     vPlayer.Play();
    34.                 }
    35.                 else
    36.                 {
    37.                     anim.Play("CameraShake", -1, 0f);
    38.                     vPlayer.Stop();
    39.                     vPlayer.Play();
    40.                 }
    41.         }
    42.     }
    43. }
     
  9. DominiqueLrx

    DominiqueLrx

    Unity Technologies

    Joined:
    Dec 14, 2016
    Posts:
    260
    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:

    Code (CSharp):
    1. vPlayer.renderMode = UnityEngine.Video.VideoRenderMode.CameraNearPlane;
    2. 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