Adding spatial audio in VR 🎧

Tuesday, 12th September 2017

Part of my Night at the Museum project is adding audio descriptions to the objects on display. To do this in Unity I’m making use of the spatial audio capabilities of the Google Cardboard SDK.

As with most things in Unity, there’s a fairly reasoned logic to how this is done. You need three main things:

  1. An audio source. You can think of this as the media player, it is literally the source of the audio within your environment
  2. An audio clip. This is the actual audio file that will be played by your audio source. So think of this as the song that will be played by your media player
  3. An audio listener. This is effectively your virtual ears within the virtual world. Normally this is attached to your main camera.

To give you an example of how I this works in practice. I first attached a GvrAudioListener to my main camera. This will allow my player to hear the sounds produced. Next, add a GvrAudioSource to a GameObject you want to produce a sound. Now load your audio source (your media player) with an audio clip. There are a few different supported formats, any old mp3 will work nicely. Just drag your MP3 into the audio click field of your GvrAudioSource. By default, the audio will play on wake, so just hit play to enter game mode and you should hear your new audio right away.

For more information on the ins and outs of Google Cardboard Audio capabilities, check out the documentation.

Pro Tip:

If you find your audio doesn’t play, try clicking edit -> project settings -> audio and make sure the “Spatializer plugin” is set to Gvr.