首页 文章

如何播放 pre-recorded 音频以使角色看起来在单位 3D 中说话,并且如果声音播放完毕则让音频播放器回拨

提问于
浏览
1

从 Pre-Recorded 音频文件播放 Unity 3D 音频使游戏角色看起来像在游戏中说话

大家好,我正在开发团结游戏,角色会在游戏中听到另一个角色的声音。音频来自以前录制并存储为资产文件。但问题是我无法检查语音是否已完成播放以继续其他操作。有人可以帮忙吗?

2 回答

  • 2

    你检查音频是否与AudioSource.isPlaying一起播放

    至于按顺序播放它们,您必须将所有音频放在 Audioclip 的数组或字典中。

    然后你可以使用一个协程来顺序播放它们并等待每一个完成AudioSource.isPlaying的播放,并通过产生该协程功能。

    下面的示例使用音频文件的名称作为单词。如果需要,您可以更改它以使用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());
    }
    
  • 0

    找到了一种方法

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

相关问题