如何播放预录制的音频以使角色看起来在Unity 3D中说话,并且如果声音播放,则会在Cheack上回调音频播放器



unity 3D音频从预录制的音频文件中播放,以使游戏字符在游戏中看起来像在游戏中

大家好,我正在研究Unity游戏,角色从游戏中的另一个角色中听取了一些声音。音频来自先前记录并存储为资产文件。但是问题是我无法检查声音以继续进行其他动作。谁能帮忙?

您检查音频是否完成了AudioSource.isPlaying

对于按顺序播放它们,您必须将所有音频放在AudioClip的阵列或字典中。

您可以使用Coroutine顺序播放它们,并等待每个人使用AudioSource.isPlaying播放,并通过产生Coroutine功能。

下面的示例使用音频文件的名称作为要讲的单词。您可以在需要时将其更改为使用Dictionary

//All the Audios to play
public AudioClip[] speech;
public AudioSource auSource;
//Converts nae of audio to the index number
int findAudio(string audioName)
{
    for (int i = 0; i < speech.Length; i++)
    {
        if (speech[i].name == audioName)
        {
            return i;
        }
    }
    return -1;
}
IEnumerator speak(string word)
{
    //Convert the string to the audioClip index 
    int audioIndex = findAudio(word);
    if (audioIndex != -1)
    {
        //Assign the clip to play
        auSource.clip = speech[audioIndex];
        //Play
        auSource.Play();
        //Wait until audio is done playing
        while (auSource.isPlaying)
        {
            yield return null;
        }
    }
}
IEnumerator PlayerSpeaker()
{
    yield return speak("Hello");
    yield return new WaitForSeconds(1f);
    yield return speak("Israel Abebe");
    yield return new WaitForSeconds(1f);
    yield return speak("How are you today?");
    yield return null;
}
// Use this for initialization
void Start()
{
    StartCoroutine(PlayerSpeaker());
}

找到了一种方法

//Declare the approprate files
public AudioClip soundFile;
AudioSource mySound;

void Awake(){
    mySound = GetComponent<AudioSource> ();
}
public bool isPlaying(AudioClip clip){
    if((Time.time - startTime) >= clip.length){
        return false;
    }
    return true;
}

您可以将语言函数用作

void Speak_func(){
        mySound.PlayOneShot (soundFile, 0.7F);
        startTime = Time.time;
    if (!isPlaying (soundFile)) {
        //what ever you want to do when speaking is done
    }
}

最新更新