private IEnumerator WaitForEndOfClipRoutine(PlayingAudio playingAudio) { yield return(new WaitForSeconds(playingAudio.source.clip.length)); Destroy(playingAudio.source); currentlyPlaying.Remove(playingAudio); }
public void Play(AudioClip clip, GameObject toPlayFrom = null, float volume = 1, float randomPitchRange = 0, bool playOnlyIfFinished = false) { if (clip == null) { return; } if (playOnlyIfFinished && ClipIsBeingPlayed(clip)) { return; } PlayingAudio newAudio = new PlayingAudio(); if (toPlayFrom != null) { StopAllSoundsFromSource(toPlayFrom); newAudio.origin = toPlayFrom; } newAudio.source = gameObject.AddComponent <AudioSource>(); newAudio.source.clip = clip; newAudio.source.volume = volume; newAudio.source.pitch = 1f + UnityEngine.Random.Range(-randomPitchRange, randomPitchRange); newAudio.source.Play(); newAudio.coroutine = StartCoroutine(WaitForEndOfClipRoutine(newAudio)); currentlyPlaying.Add(newAudio); }
private void OnPlaySound(object sender, EventArgs e) { try { if (!SubtitleConfig.EnableSubtitle) { return; } if (CurrentScene.name != "SceneYotogi") { return; } var audioManager = sender as AudioSourceMgr; if (audioManager == null) { return; } if (audioManager.SoundType != AudioSourceMgr.Type.VoiceHeroine) { return; } lock (PlayingAudioSource) { Logger.Log(LogLevel.Debug, $"Now playing {audioManager.FileName}"); PlayingAudioSource[audioManager.FileName] = audioManager.audiosource; if (PlayingAudio.Contains(audioManager.FileName)) { PlayingAudio.Remove(audioManager.FileName); } PlayingAudio.AddFirst(audioManager.FileName); } } catch (Exception ex) { Logger.Log(LogLevel.Error, ex); throw; } }