1

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

derHugo
  • 83,094
  • 9
  • 75
  • 115
ElRosbif
  • 353
  • 1
  • 6
  • 18
  • Possible duplicate of [Using new Unity VideoPlayer and VideoClip API to play video](https://stackoverflow.com/questions/41144054/using-new-unity-videoplayer-and-videoclip-api-to-play-video) – Programmer Sep 18 '17 at 12:07
  • Seriously dude, you have to call `videoPlayer.Prepare();` after `SetTargetAudioSource`. See the duplicate. You linked to a working answer but then made a mistake. If you can't get it to work then use the code from duplicate answer like it is. Don't modify it. – Programmer Sep 18 '17 at 12:10

1 Answers1

2

Through trial and error, here's the minimum that I've found that needs to be called to play a VideoClip from C# using Unity 2017.1 on Windows 10:

public class VideoPlayerTest : MonoBehaviour {

   public VideoPlayer player;
   public VideoClip clip;
   public AudioSource audioSource;

   // Use this for initialization
   void Start () {
       player.audioOutputMode = VideoAudioOutputMode.AudioSource;
       player.SetTargetAudioSource(0, audioSource);
       player.source = VideoSource.VideoClip;
       player.clip = clip;
       player.Play();
   }
}

Using a GameObject configured like this: Unity Editor Inspector With this MP4 file (download and drop into your Assets directory): https://www.quirksmode.org/html5/videos/big_buck_bunny.mp4

Good luck! I spent a lot of time fiddling with enabling tracks/channels and it didn't seem to do anything before stumbling upon this simple configuration.

Robert Taylor
  • 77
  • 1
  • 3