//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());
}
void Speak_func(){
mySound.PlayOneShot (soundFile, 0.7F);
startTime = Time.time;
if (!isPlaying (soundFile)) {
//what ever you want to do when speaking is done
}
}
2 回答
你检查音频是否与AudioSource.isPlaying一起播放
至于按顺序播放它们,您必须将所有音频放在 Audioclip 的数组或字典中。
然后你可以使用一个协程来顺序播放它们并等待每一个完成
AudioSource.isPlaying
的播放,并通过产生该协程功能。下面的示例使用音频文件的名称作为单词。如果需要,您可以更改它以使用
Dictionary
。找到了一种方法
你可以使用说话功能