unity AudioSource播放完聲音後要執行的函數或條件操作

將腳本掛在要判斷聲音是否播放完畢的物體上

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()
{
//按下鍵盤按鈕A鍵執行函數播放語音
if (Input.GetKeyDown(KeyCode.A))
{
PlayAudio(GameObject.GetComponent().clip)
}
}
//接受音頻文件和是否重復播放
public void PlayAudio(AudioClip clip, UnityAction callback = null, bool isLoop = false)
{
//獲取自身音頻文件進行播放並且不重復播放
_audio.clip = clip;
_audio.loop = isLoop;
_audio.Play();
//執行協成獲取音頻文件的時間
StartCoroutine(AudioPlayFinished(_audio.clip.length, callback));
}
//執行協成函數 並且返回時間
private IEnumerator AudioPlayFinished(float time, UnityAction callback)
{   
    yield return new WaitForSeconds(time);
    //聲音播放完畢後之下往下的代碼  
    # region   聲音播放完成後執行的代碼
    print("聲音播放完畢,繼續向下執行"); 
    #endregion
}
}

補充:Unity 的 AudioSourse 播完的監聽

最近涉及到 音頻結束後的調用問題,unity 原生的音頻組件 AudioSourse 沒有功能,於是自己寫瞭一個。

下面是代碼:

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,
}
 

補充:Unity3d AudioSource如何監聽播放完成並處理邏輯

想知道AudioSource什麼時候播放完成並處理相關的邏輯,比如切換曲目,而unity又沒有提供相應的事件

於是想到下面幾種方案:

1、Update時時判斷isPlaying

2、獲取音頻的播放長度,Invoke一下

後來查看api的時候突然想到,可以用協程啊。原理和Invoke一樣,這應該是最好的方案瞭。

不過如果音頻暫停掉瞭之後而又沒有更新協程函數的話,問題就出現瞭。所以暫停的時候記得更新協程函數。

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀:

    None Found