AudioSource and AudioListener
- Game Development course
- Intermediate
- Lesson 25/83
- 4 min
Learn the division of labor between AudioSource and AudioListener.
AudioSource and AudioListener
Unity's audio engine operates on a speaker and ear division of labor. AudioListener represents the player's ears and listens to ambient sounds; it is attached to Main Camera by default and typically exactly 1 should exist in a scene.
// AudioListener component on main camera
AudioListener listener = Camera.main.GetComponent<AudioListener>();Having multiple active AudioListeners generates editor warnings and can lead to incorrect audio playback.
AudioSource speaker
AudioSource is the speaker component that broadcasts sound waves from a GameObject in the scene. To play footsteps, engine hums, or ambient music, this component is attached and linked to an AudioClip asset.
AudioSource audioSource = gameObject.AddComponent<AudioSource>();
audioSource.clip = explosionClip;The AudioSource component establishes the spatial origin of audio within the 3D game world.
Practice Print audio emitter and listener info
Assign "AudioSource" to source and "AudioListener" to listener. Print Emitter: AudioSource | Receiver: AudioListener to the console.
Starter code
using System;
class Program
{
static void Main()
{
// source ve listener değerlerini tanımlayıp yazdır
}
}Sample solution
using System;
class Program
{
static void Main()
{
string source = "AudioSource";
string listener = "AudioListener";
Console.WriteLine("Emitter: " + source + " | Receiver: " + listener);
}
}Expected output
Emitter: AudioSource | Receiver: AudioListener
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which component acts as the virtual ear of the player and commonly resides on the Main Camera in Unity?
- MeshCollider
- Rigidbody
- AudioMixerSnapshot
- AudioListener Correct answer
Explanation: AudioListener is the virtual ear component gathering audio waves in the scene to deliver them to the player's physical speakers.
2 Add AudioSource component
AudioSource source = gameObject.AddComponent<____>();
AudioSource source = gameObject.AddComponent<AudioSource>();
Explanation: To grant an object audio emitting capability, an AudioSource component is added.
3 Core audio components
| Term | Meaning |
|---|---|
AudioListener |
Listener routing sounds to speakers |
AudioSource |
Speaker component emitting sound waves |
AudioClip |
File holding raw audio data |
Main Camera |
Default object with AudioListener |