Introduction to AudioSource

Introduction to AudioSource

AudioSource is one of the components used to play audio in Unity. It can be attached to a game object and programmatically controlled to play, pause, stop audio, etc. AudioSource can be used to play various types of audio such as background music, sound effects, and dialogues.

AudioSource method

1. Play()

The Play() method is used to start playing audio. The loaded and set audio clip can be played by calling this method.

Sample code:

AudioSource audioSource;
audioSource.Play();

2. Stop()

The Stop() method is used to stop the audio being played. After calling this method, the audio will stop playing and return to the starting position.

Sample code:

AudioSource audioSource;
audioSource.Stop();

3. Pause()

The Pause() method is used to pause the currently playing audio. After calling this method, the audio will be paused at the current position and can be resumed by calling the Play() method.

Sample code:

AudioSource audioSource;
audioSource.Pause();

4. volume

The volume attribute is used to control the volume of the audio. The value range is from 0.0 to 1.0, where 0.0 means mute and 1.0 means maximum volume.

Sample code:

AudioSource audioSource;
audioSource.volume = 0.5f;

5. loop

The loop attribute is used to set whether the audio is played in a loop. When set to true, the audio will loop; when set to false, the audio will stop after playing once.

Sample code:

AudioSource audioSource;
audioSource.loop = true;

AudioSource example

The following are several common code examples using AudioSource:

play sound

using UnityEngine;

public class SoundManager : MonoBehaviour
{
    
    
    public AudioSource audioSource;
    public AudioClip soundEffect;

    public void PlaySoundEffect()
    {
    
    
        audioSource.clip = soundEffect;
        audioSource.Play();
    }
}

play background music

using UnityEngine;

public class AudioManager : MonoBehaviour
{
    
    
    public AudioSource audioSource;
    public AudioClip backgroundMusic;

    private void Start()
    {
    
    
        audioSource.clip = backgroundMusic;
        audioSource.Play();
    }
}

volume fade

using UnityEngine;

public class VolumeController : MonoBehaviour
{
    
    
    public AudioSource audioSource;
    public float fadeDuration = 1.0f;
    public float targetVolume = 0.5f;

    public void FadeVolume()
    {
    
    
        StartCoroutine(FadeVolumeCoroutine());
    }

    private IEnumerator FadeVolumeCoroutine()
    {
    
    
        float startVolume = audioSource.volume;
        float startTime = Time.time;

        while (Time.time - startTime < fadeDuration)
        {
    
    
            float t = (Time.time - startTime) / fadeDuration;
            audioSource.volume = Mathf.Lerp(startVolume, targetVolume, t);
            yield return null;
        }

        audioSource.volume = targetVolume;
    }
}

Hope the above examples can help you better understand and use AudioSource components.

Guess you like

Origin blog.csdn.net/qq_20179331/article/details/132002716