4 using System.Collections.Generic;
7 namespace HKI.Core.Audio
24 SingleOutputWithCrossfade,
30 [SerializeField]
float FadingTimeInSeconds = 5.0f;
31 [SerializeField]
bool AtSetupStartPlayingPlaylist =
true;
32 [SerializeField]
Playlist StartPlaylist = null;
34 [SerializeField]
UnityEngine.Audio.AudioMixerGroup AudioMixerGroup = null;
35 [SerializeField]
Language LocalizationLanguage = null;
37 [SerializeField]
bool DontGetDestroyedOnLoad =
false;
40 public Playmodes GetPlaymode {
get {
return Playmode; } }
41 public float GetFadingTimeInSeconds {
get {
return FadingTimeInSeconds; } }
42 public bool GetAtSetupStartPlayingPlaylist {
get {
return AtSetupStartPlayingPlaylist; } }
43 public Playlist GetStartPlaylist {
get {
return StartPlaylist; } }
45 public UnityEngine.Audio.AudioMixerGroup GetAudioMixerGroup {
get {
return AudioMixerGroup; } }
46 public Language GetLocalizationLanguage {
get {
return LocalizationLanguage; } }
48 public bool GetDontGetDestroyedOnLoad {
get {
return DontGetDestroyedOnLoad; } }
53 StringBuilder infoString =
new StringBuilder();
54 infoString.AppendLine(
"Audio Player Settings:");
56 infoString.Append(
"Playmode: ");
57 infoString.AppendLine(Playmode.ToString());
59 infoString.Append(
"FadingTimeInSeconds: ");
60 infoString.AppendLine(FadingTimeInSeconds.ToString());
62 infoString.Append(
"AtSetupStartPlayingPlaylist: ");
63 infoString.AppendLine(AtSetupStartPlayingPlaylist.ToString());
65 infoString.Append(
"StartPlaylist: ");
66 infoString.AppendLine(StartPlaylist != null ? StartPlaylist.name :
"null");
68 infoString.Append(
"AudioMixerGroup: ");
69 infoString.AppendLine(AudioMixerGroup != null ? AudioMixerGroup.name :
"null");
71 infoString.Append(
"LocalizationLanguage: ");
72 infoString.AppendLine(LocalizationLanguage != null ? LocalizationLanguage.name :
"null");
74 infoString.Append(
"DontGetDestroyedOnLoad: ");
75 infoString.AppendLine(DontGetDestroyedOnLoad.ToString());
77 return infoString.ToString();
84 List<AudioSource> audioSources =
new List<AudioSource>();
85 int currentMainAudioSourceId = 0;
87 bool isPlayingPlaylist =
false;
90 Coroutine crossfadeCoroutine = null;
91 Coroutine playNextSoundClipOfPlaylistCoroutine = null;
96 public bool IsPlayingPlaylist {
get {
return isPlayingPlaylist; } }
97 public Playlist GetCurrentPlaylist {
get {
return currentPlaylist; } }
104 Debug.LogError(
"(SimpleAudioPlayer)" + gameObject.name +
" you try to setup the (AudioPlayer) but the new (AudioPlayerSettings) settings is null!");
108 StopPlayingAllClips();
110 if(audioSources != null && audioSources.Count > 0)
112 for (
int i = 0; i < audioSources.Count; i++)
113 Destroy(audioSources[i]);
115 audioSources.Clear();
118 currentSettings = settings;
125 audioSources.Add(CreateAudioSource());
129 audioSources.Add(CreateAudioSource());
130 audioSources.Add(CreateAudioSource());
135 DontDestroyOnLoad(gameObject);
143 if(playPlaylist && currentPlaylist == null && debugLog)
145 Debug.LogError(
"(SimpleAudioPlayer)" + gameObject.name +
" you try to play a clip from the current playlist but the current playlist is null!");
151 Debug.LogWarning(
"(SimpleAudioPlayer)" + gameObject.name +
" you try to play a clip from the current playlist " + currentPlaylist.name +
" but the current playlist has no sound clips in it!");
155 if (!isPlayingPlaylist && playPlaylist)
157 isPlayingPlaylist = playPlaylist;
160 else if(isPlayingPlaylist && !playPlaylist)
162 isPlayingPlaylist = playPlaylist;
163 StopPlayingAllClips();
171 Debug.LogError(
"(SimpleAudioPlayer)" + gameObject.name +
" you try to change the (Playlist) but the new playlist is null!");
175 currentPlaylist = playlist;
177 if(isPlayingPlaylist)
183 PlayPlaylist(playPlaylist);
191 Debug.LogError(
"(SimpleAudioPlayer)" + gameObject.name +
" you try to play a (SoundClip) but the clip is null!");
195 if(currentSettings == null || audioSources.Count == 0)
197 Debug.LogError(
"(SimpleAudioPlayer)" + gameObject.name +
" you try to play a (SoundClip) but the AudioPlayer isn't setuped!");
207 if(audioLocalization == null)
209 Debug.LogError(
"(SimpleAudioPlayer)" + gameObject.name +
" you try to play a (AudioLocalization) but the audioLocalization is null!");
213 if(currentSettings == null || audioSources.Count == 0)
215 Debug.LogError(
"(SimpleAudioPlayer)" + gameObject.name +
" you try to play a (AudioLocalization) but the audio player isn't setuped!");
221 Debug.LogError(
"(SimpleAudioPlayer)" + gameObject.name +
" you try to play a (AudioLocalization) but the audio player setting has no localization language!");
227 if(audioLocalizationElement != null)
228 PlayAudioClip(audioLocalizationElement.GetClip, audioLocalizationElement.GetVolume, audioLocalizationElement.GetPitch.GetRandomValue());
235 Debug.LogError(
"(SimpleAudioPlayer)" + gameObject.name +
" you try to play a (AudioClip) but the clip is null!");
239 if(currentSettings == null || audioSources.Count == 0)
241 Debug.LogError(
"(SimpleAudioPlayer)" + gameObject.name +
" you try to play a (AudioClip) but the audio player isn't setuped!");
248 if(playNextSoundClipOfPlaylistCoroutine != null)
249 StopCoroutine(playNextSoundClipOfPlaylistCoroutine);
251 audioSources[0].clip = clip;
252 audioSources[0].volume = volume;
253 audioSources[0].pitch = pitch;
255 audioSources[0].Play();
257 if(isPlayingPlaylist)
258 playNextSoundClipOfPlaylistCoroutine = StartCoroutine(PlayNextSoundClipOfPlaylist(clip.length));
262 if(crossfadeCoroutine != null)
263 StopCoroutine(crossfadeCoroutine);
265 if(playNextSoundClipOfPlaylistCoroutine != null)
266 StopCoroutine(playNextSoundClipOfPlaylistCoroutine);
268 crossfadeCoroutine = StartCoroutine(PlayClipWithCrossfade(clip, volume, pitch));
270 if(isPlayingPlaylist)
271 playNextSoundClipOfPlaylistCoroutine = StartCoroutine(PlayNextSoundClipOfPlaylist(clip.length - currentSettings.
GetFadingTimeInSeconds));
275 AudioSource freeAudioSource = null;
277 for(
int i = 0; i < audioSources.Count; i++)
279 if (!audioSources[i].isPlaying)
280 freeAudioSource = audioSources[i];
283 if(freeAudioSource == null)
285 freeAudioSource = CreateAudioSource();
286 audioSources.Add(freeAudioSource);
289 freeAudioSource.clip = clip;
290 freeAudioSource.volume = volume;
291 freeAudioSource.pitch = pitch;
293 freeAudioSource.Play();
301 isPlayingPlaylist =
false;
303 if(playNextSoundClipOfPlaylistCoroutine != null)
304 StopCoroutine(playNextSoundClipOfPlaylistCoroutine);
306 for (
int i = 0; i < audioSources.Count; i++)
308 audioSources[i].Stop();
309 audioSources[i].clip = null;
310 audioSources[i].volume = 0.0f;
311 audioSources[i].pitch = 1.0f;
318 AudioSource audioSource = gameObject.AddComponent<AudioSource>();
320 audioSource.playOnAwake =
false;
321 audioSource.loop =
false;
322 audioSource.clip = null;
323 audioSource.volume = 0.0f;
324 audioSource.pitch = 1.0f;
331 AudioSource oldAudioSource = audioSources[currentMainAudioSourceId];
332 currentMainAudioSourceId = (currentMainAudioSourceId + 1) % 2;
333 AudioSource newAudioSource = audioSources[currentMainAudioSourceId];
335 newAudioSource.clip = clip;
336 newAudioSource.volume = 0.0f;
337 newAudioSource.pitch = pitch;
339 newAudioSource.Play();
346 while (remainingFadeTime > 0.0f)
348 oldAudioSource.volume -= Time.unscaledDeltaTime * oldVolumeChangePerSecond;
349 newAudioSource.volume += Time.unscaledDeltaTime * newVolumeChangePerSecond;
351 remainingFadeTime -= Time.unscaledDeltaTime;
355 newAudioSource.volume = volume;
357 oldAudioSource.Stop();
358 oldAudioSource.clip = null;
359 oldAudioSource.volume = 0.0f;
360 oldAudioSource.pitch = 1.0f;
365 yield
return new WaitForSecondsRealtime(waitTime);
367 if(isPlayingPlaylist)
This ScriptableObject contains the data of a playlist for the AudioPlayer.
void PlayPlaylist(bool playPlaylist, bool debugLog=true)
void ChangePlaylist(Playlist playlist)
void PlayAudioLocalization(AudioLocalization audioLocalization)
void Setup(AudioPlayerSettings settings)
void PlaySoundClip(SoundClip clip)
void PlayAudioClip(AudioClip clip, float volume, float pitch)
bool GetAtSetupStartPlayingPlaylist
void StopPlayingAllClips()
This subclass holds the data for a audio localization in a specific language.
This MonoBehaviour allows playing of a SoundClip on one of the general purpose AudioPlayers.
This is a AudioPlayer that can play playlist or SoundClips. It allows to play multiple clips at once ...
float GetFadingTimeInSeconds
Special Variable enables the use of the SystemLanguage data type as a variable of the settings system...
This MonoBehaviour allows the changing of the playlist on one of the general purpose AudioPlayers...
IEnumerator PlayClipWithCrossfade(AudioClip clip, float volume, float pitch)
Language GetLocalizationLanguage
Playlist GetStartPlaylist
AudioLocalizationElement GetAudioLocalizationElement(Language language)
This function returns the AudioLocalizationElement in a specific language. If there is no localizatio...
This is a container ScriptableObject that holds all data required to play a AudioClip.
UnityEngine.Audio.AudioMixerGroup GetAudioMixerGroup
bool GetDontGetDestroyedOnLoad
void ChangePlaylist(Playlist playlist, bool playPlaylist)
IEnumerator PlayNextSoundClipOfPlaylist(float waitTime)
AudioSource CreateAudioSource()
This ScriptableObject contains as a container for all audio localization information.