Implementing Unity Android Audio without delay

July 28, 2019


In the above blog, I discussed the issue with Unity’s default audio system when it
comes to playing sounds in Android devices.
In this article, we will get to the conclusion of this problem & get this issue solved by using Unity’s native android audio system to play sounds.


Setting Up Unity’s Native Android Audio System

To get started, we will set up the Android audio plugin in our Unity project.
We are using the plugin from this git repository:
Download the Plugin from AudioBypassPlugin and extract the zip file
This plugin is a part of this git repository: GitHub
[NOTE: Follow the same names as described here else this will not work]
  1. In your Unity Project’s folder view, create (if not exists) a folder named Plugins at the root.
  2. Inside Plugins, create (if not exists) another folder named Android
  3. Drag & Drop the downloaded file to Android Folder
  4. Now create another folder named StreamingAssets adjacent to Plugins folder, such that StreamingAssets folder is at the root location.

Working of Android Native Audio System

The Android native audio system needs to register sounds to play them. For each registered sound, we get an integer id which we will use again & again to play that sound.
For example, for a button click sound, let’s say we get an integer id of 1. Now, we will use this id to play this sound whenever required like PlaySound(1).

Playing Sounds in Unity Android Project

To get started, I have created a boilerplate project which has the setup points completed & also have a canvas containing a button that we will code to play a click sound whenever the button is pressed.
You can download the boilerplate project here

Coding the Play Button

Download & Open the boilerplate project in Unity.
Note that the scene has an UnityAudioManager & a disabled NativeAudioManager gameobject in the Project Hierarchy
The boilerplate already has the code for playing the click sound with Unity’s default audio system.
We will now write code to play audio using the Android Native method.
To play Android native sound, we have to place our audio file in StreamingAssets folder, as existing in the boilerplate project.

Beginning with Code

  1. Enable the NativeAudioManager in the project hierarchy.
  2. In the Native Audio directory, open the C# script named NativeAudioSystemClick.cs. This is an empty code file & we will code it now.
Import the following namespace:
using AndroidAudioBypass;
Create a field for holding the reference of the AudioManager.
[SerializeField] BypassAudioManager AudioManagerInstance;
Create a field for holding the integer id of the registered button click sound & default it to -1 ( negative one).
int buttonClickSoundId = -1;
Create a field of string type holding the name of the mp3 file to be played in the StreamingAssets folder.
string soundFileName = "buttonclick.mp3";
In the script’s Start() method, register the button's click sound file.
buttonClickSoundId = AudioManagerInstance.RegisterSoundFile(soundFileName);
Now add a method that we will trigger from the button click event:
public void PlayButtonClickSound() {
  AudioManagerInstance.PlaySound(buttonClickSoundId, 1, 1, 1, 0, 1); 
}
One last thing, In Hierarchy view, select NativeAudioButton inside canvas & select the method that we just created.

The android native audios will not play in the Unity’s game view. They will only run in a real device
 Now build and run the apk in an android device/emulator to see the button click without any delay.

You Might Also Like

0 comments

Follow by Email