Exercise - Spatialize audio from a video
In this unit, you'll learn how to spatialize audio from a video source and test this in the Unity editor and HoloLens 2.
Import a video and add a video player to the scene
For this unit, you can use this video from the spatial audio sample project.
To import the video into the unity project, select Assets > Import New Asset in the Unity menu.
In the Import New Asset... window, select the Microsoft HoloLens - Spatial Sound-PTPvx7mDon4 file you downloaded and select the Import button to import the asset into the project.
Adjusting the quality settings on the video clip can ensure smooth playback on HoloLens 2. Select the video file in the Project window. In the Inspector window, select the Windows tab, check the box for Override for Windows, and configure the rest of the settings as follows:
- Check the box for Transcode.
- Set Codec to H264.
- Set Bitrate Mode to Low.
- Set Spatial Quality to Medium Spatial Quality.
Select Apply to change the quality setting on the video clip.
Right-click in the Hierarchy window, then select Video > Video Player to add the Video player component.
Play video onto a quadrangle
The Video Player object needs a textured game object to render the video.
Right-click in the Hierarchy window, then select 3D Object > Quad to create a quad and configure its Transform component as follows:
- Position: X = 0, Y = 2.1, Z = 2
- Rotation: X = 0, Y = 0, Z = 0
- Scale: X = 1.28, Y = 0.72, Z = 1
Now you need to texture the Quad with the video. Right-click in the Project window and choose Create > Render Texture to create a Render Texture component. Enter a suitable name to the Render Texture; for example, Spatial Audio Texture.
Select the Render Texture and in the Inspector window set the Size property to match the video's native resolution of 1280x720. Then, to ensure good rendering performance on HoloLens 2, set the Depth Stencil Format property to D16_UNORM or higher.
Next, use the created Render Texture Spatial Audio Texture as the texture for the Quad:
- Drag the Spatial Audio Texture from the Project window onto the Quad in the Hierarchy to add the Render Texture to the Quad.
- To ensure good performance on HoloLens 2, select the Quad in the Hierarchy and in the Inspector window, select Graphics Tools > Standard in the Shader field.
To set Video Player and Render Texture to play the video clip, select the Video Player in the Hierarchy and configure the settings in the Inspector window as follows:
- Set the Video Clip property to the downloaded video file: Microsoft HoloLens - Spatial Sound-PTPvx7mDon4.
- Check the Loop checkbox.
- Set the Target Texture to your new render texture, Spatial Audio Texture.
Spatialize the audio from the video
In the Hierarchy window, select the Quad object. In the Inspector window, use the Add Component button to add an Audio Source, to which you'll route the audio from the video.
In the Audio Source:
- Set Output to the Spatial Audio Mixer > Master.
- Check the Spatialize box.
- Move the Spatial Blend slider to 1 (3D).
To set the Video Player to route its audio to the Audio Source, select the Video Player in the Hierarchy window. In the Inspector window, configure the Video Player object as follows:
- Set the Audio Output Mode to Audio Source
- Set the Audio Source property to the Quad