Function or conditional operation to be performed after the sound is played by unity audiosource

Time:2022-5-23

Hang the script on the object to judge whether the sound has been played

using System.Collections;
using UnityEngine;
using UnityEngine.Events;
[RequireComponent(typeof(AudioSource))]
public class AudioManager : MonoBehaviour
{
public static AudioManager instence = null;
private AudioSource _audio;
void Awake()
{
if (instence == null)
{
instence = this;
}
}
void Start()
{
    _audio = GetComponent<AudioSource>();
  
}
void Update()
{
//Press the keyboard button a to execute the function and play the voice
if (Input.GetKeyDown(KeyCode.A))
{
PlayAudio(GameObject.GetComponent().clip)
}
}
//Accept audio files and repeat
public void PlayAudio(AudioClip clip, UnityAction callback = null, bool isLoop = false)
{
//Get your own audio file to play without repeating
_audio.clip = clip;
_audio.loop = isLoop;
_audio.Play();
//Time of executing collaboration to obtain audio files
StartCoroutine(AudioPlayFinished(_audio.clip.length, callback));
}
//Execute the cointegration function and return the time
private IEnumerator AudioPlayFinished(float time, UnityAction callback)
{   
    yield return new WaitForSeconds(time);
    //The code below after the sound is played  
    #Region code executed after sound playback
    Print ("after the sound is played, continue to execute downward"); 
    #endregion
}
}

Supplement: unity‘s audiosourse is listening after broadcasting

Recently, when it comes to the call after the end of audio, the native audio component audiosource of unity has no function, so I wrote one myself.

Here is the code:


using Assets.Scripts.Entities;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine; 
public class AudioSourceInfo
{
    private float playTime = 0;
    public AudioSource AudioSource { get; private set; }
    public AudioState AudioState = AudioState.Idle;
    public Action AfterPlaying { get; set; }
    public int ID = 0;
    public AudioSourceInfo(GameObject go)
    {
        this.AudioSource = go.AddComponent<AudioSource>();
    }
    public AudioClip Clip
    {
        get
        {
            return this.AudioSource.clip;
        }
        set
        {
            this.AudioSource.clip = value;
            playTime = 0;
        }
    }
    public bool Loop
    {
        get
        {
            return this.AudioSource.loop ;
        }
        set
        {
            this.AudioSource.loop = value;
        }
    }
    public float Volume
    {
        get
        {
            return this.AudioSource.volume;
        }
        set
        {
            this.AudioSource.volume = value;
        }
    }
    public void Play()
    {
        if (null == this.AudioSource)
        {
            return;
        }
        this.AudioState = AudioState.IsPlaying;
        this.AudioSource.Play();
    }
    public void Pause()
    {
        if (null == this.AudioSource)
        {
            return;
        }
        if(this.AudioSource.isPlaying)
        {
            this.AudioState = AudioState.Pause;
            this.AudioSource.Pause();
        }
    }
    public void Stop()
    {
        if (null == this.AudioSource)
        {
            return;
        }
        this.AudioState = AudioState.Stop;
        this.AudioSource.Stop();
        if(AfterPlaying!= null)
        {
            this.AfterPlaying();
        }
    }
    private void Update()
    {
        if (this.AudioSource != null &&  this.AudioSource.clip!= null && this.AudioState == AudioState.IsPlaying)
        {
            playTime += Time.fixedDeltaTime;
            if (playTime >= this.Clip.length)
            {
                playTime = 0;
                this.Stop();
            }
        }
    }
} 
public enum AudioState
{
    Idle,
    IsPlaying,
    Pause,
    Stop,
}
 

Supplement: how does unity3d audiosource monitor the completion of playback and process logic

I want to know when audiosource will finish playing and deal with relevant logic, such as switching tracks, but unity does not provide corresponding events

So I thought of the following schemes:

1. Update isplaying

2. Get the playback length of the audio and invoke it

Later, when I checked the API, I suddenly thought that you can use collaborative process. The principle is the same as invoke, which should be the best solution.

However, if the audio is paused and the coprocessor function is not updated, the problem occurs. So remember to update the coprocessor function when pausing.

The above is my personal experience. I hope I can give you a reference, and I hope you can support developpaer. If there are mistakes or not fully considered, please don’t hesitate to comment.