Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Adding Video With Sound In Unity Games

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.45k
    Comment on it

    Playing video file in unity game is as simple as drag and drop.
    The only requirement is that the video that you have to add must be in .mov, .mpg, .mpeg, .mp4, .avi, .asf formats.
    Then simply put video file(.mp4,.avi etc) in unity assets folder and then take UI Raw Image in game.
    To play video file just drag video file from assets folder to Texture field of raw image as shown in image.

     

    After this create a script with following line of code :-

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    using UnityEngine.Events;
    [RequireComponent (typeof(AudioSource))]
    
    public class PlayVideo : MonoBehaviour {
    
    	public MovieTexture movieTexture;
    
    	// Use this for initialization
    	void Start () 
    	{
    		movieTexture.Play ();
    
    	}
    	
    	
    }

     

    Add above script to main camera. Drag "video" file to MovieTexture field of the script. With this you would be able to play video in game but without sound.

    So in order to play audio with video you have to follow few more steps as given below.

    The video file that you put earlier in Assets folder must contain an Audio file as a child.You have to use that audio file to play audio.Take an empty gameobject and drag the audio child  to it. Now this is how we w'll play audio . Same "PlayVideo" script with some changes will paly audio for you.

     

    using UnityEngine;
    using UnityEngine.UI;
    using System.Collections;
    using UnityEngine.Events;
    [RequireComponent (typeof(AudioSource))]
    
    public class PlayVideo : MonoBehaviour 
    {
    
    	public MovieTexture movieTexture;
    
    	public AudioSource audio;
    
    	//AudioClip audio;
    	// Use this for initialization
    	void Start () 
    	{
    		movieTexture.Play ();
    
    		audio.clip = movieTexture.audioClip;
    		
    		audio.Play ();
    
    	}
    	
    }

    Drag "video" file to "movieTexture" field as before and "empty gameobject that contain audio child of the video" file to "audio" field and you have a "playable video with audio" in game on Play. See the image given below for help.

    Thanks!Have a nice day :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: