Documentation for the Unity C# Library
Loading...
Searching...
No Matches
AudioService.cs
Go to the documentation of this file.
2using System;
3using System.Collections;
4using UnityEngine;
5
6namespace PixoVR.Audio
7{
11 public class AudioService : MonoBehaviour
12 {
13 [SerializeField] private VoiceOverData voices;
14 [SerializeField] private VoiceOverData soundsFx;
17 public RepeatableAudio LastPlayedRepeatableAudio { get; private set; }
18 public RepeatableAudio PlayOnceAudio { get; private set; }
19
20 private const float _soundRepeatDelay = 30;
21 private const float _mutedVolume = 0.001f;
22 private bool _isSoundMuted = false;
23
24 private Coroutine _playRepeatableCoroutine;
25
26 public event Action RepeatableAudioStopped;
27 public event Action<string> OnceTimeAudioStopped;
28
31 private void Start()
32 {
33 EazySoundManager.AudioStopped += OnAudioStopped;
34 }
36 private void OnDestroy()
37 {
38 EazySoundManager.AudioStopped -= OnAudioStopped;
39 }
46 {
47 voices = voData;
48 }
49
56 public VoiceOverInfo GetOverOverData(string voiceOverName)
57 {
58 var voiceOverData = voices.GetDataVO(voiceOverName);
59
60 if (voiceOverData == null)
61 {
62 Debug.LogError("Voice over data is not exist");
63 }
64
65 return voiceOverData;
66 }
67
73 public bool IsPlaying(string fxName)
74 {
75 var clip = soundsFx.GetAudioClipVO(fxName);
76
77 if (clip == null)
78 {
79 Debug.LogError("audio clip not found " + fxName);
80 return false;
81 }
83 var audio = EazySoundManager.GetAudio(clip);
84 return audio != null ? audio.IsPlaying : false;
85 }
86
92 public int PlayFX(string fxName)
93 {
94 var clip = soundsFx.GetAudioClipVO(fxName);
95
96 if (clip == null)
97 {
98 Debug.LogError("audio clip not found " + fxName);
99 return -1;
100 }
102 return EazySoundManager.PlaySound(clip, fxName);
103 }
104
111 public void PlayFX(string fxName, bool loop)
112 {
113 var clip = soundsFx.GetAudioClipVO(fxName);
114
115 if (clip == null)
116 {
117 Debug.LogError("audio clip not found " + fxName);
118 return;
119 }
121 EazySoundManager.PlaySound(clip, fxName, loop);
122 }
123
131 public void PlayFX(string fxName, float volume, bool loop)
132 {
133 var clip = soundsFx.GetAudioClipVO(fxName);
134
135 if (clip == null)
136 {
137 Debug.LogError("audio clip not found " + fxName);
138 return;
139 }
141 EazySoundManager.PlaySound(clip, fxName, volume, loop);
142 }
143
144
152 public int PlaySpatiallyFX(string fxName, float volume, bool loop, Transform sourceTransform)
153 {
154 Debug.Log($"TestAudio PlaySpatiallyFX {fxName}");
155 var fxInfo = soundsFx.GetDataVO(fxName);
156
157 if (fxInfo == null)
158 {
159 Debug.LogError("voice over info not found " + fxName);
160 return -1;
162
163 var audioId = EazySoundManager.PlaySound(fxInfo.voiceOver, fxName, volume, loop, sourceTransform);
164
165 var spatialSoundSettings = GetSpatialSoundSettings(fxInfo);
166
167 if (spatialSoundSettings == null) return -1;
168
169 ConfigureAudioSource(EazySoundManager.GetAudio(audioId), spatialSoundSettings);
170
171 return audioId;
172 }
173
174 private void ConfigureAudioSource(Hellmade.Sound.Audio audio, SpatialSoundSettings spatialSoundSettings)
175 {
176 audio.RolloffMode = spatialSoundSettings.audioRolloffMode;
177 audio.Max3DDistance = spatialSoundSettings.maxDistance;
178 audio.Min3DDistance = spatialSoundSettings.minDistance;
179 audio.SpatialBlend = 1f;
180 }
181
182 private SpatialSoundSettings GetSpatialSoundSettings(VoiceOverInfo voiceOverInfo)
184 if (voiceOverInfo.UseSpatialSoundSettings)
185 {
186 if (voiceOverInfo.spatialSoundSettings != null)
187 return voiceOverInfo.spatialSoundSettings;
188 else
189 {
190 Debug.LogError("Can't use SpatialSoundSettings because it is null");
191 return null;
192 }
193 }
194 else
195 {
196 if (defaultSpatialSoundSettings != null)
198 else
199 {
200 Debug.LogError("defaultSpatialSoundSettings is null");
201 return null;
202 }
203 }
204 }
205
211 public void SetSfxVolume(int audioID, float volume)
212 {
213 EazySoundManager.GetAudio(audioID)?.SetVolume(volume);
214 }
215
222 public void SetSfxVolume(string name, float volume)
223 {
224 var fx = EazySoundManager.GetAudio(name);
225
226 if (fx == null)
227 {
228 Debug.LogWarning("Fx not found");
229
230 return;
232
233 fx.SetVolume(volume);
234 }
235
236
242 public void SetSfxPitch(int audioID, float pitch)
243 {
244 Hellmade.Sound.Audio audioInstance = EazySoundManager.GetAudio(audioID);
245 if (audioInstance != null)
246 {
247 audioInstance.Pitch = pitch;
248 }
249 }
250
256 public void StopFx(string fxName)
257 {
258 var clip = soundsFx.GetAudioClipVO(fxName);
259
260 if (clip == null)
261 {
262 Debug.LogError("audio clip not found " + fxName);
263 return;
264 }
266 var audio = EazySoundManager.GetAudio(clip);
267
268 if (audio != null)
269 {
270 audio.Stop();
271 }
272 }
273
279 public void StopFxById(int fxId)
280 {
281 var audio = EazySoundManager.GetSoundAudio(fxId);
282
283 if (audio == null)
284 {
285 Debug.LogWarning($"Fx id - {fxId} is not exist");
286
287 return;
289
290 audio.Stop();
291 }
292
297 public void PlayOnlyOneFX(string fxName)
298 {
299 StopAllFx();
300
301 var clip = soundsFx.GetAudioClipVO(fxName);
302
303 if (clip == null)
304 {
305 Debug.LogError("audio clip not found " + fxName);
306 return;
307 }
308
309 EazySoundManager.PlaySound(clip, fxName);
310 }
311
315 public void StopPlaying()
316 {
317 StopAllFx();
318 }
319
325 public void PlayVoice(string voiceOver)
326 {
327 var clip = voices.GetAudioClipVO(voiceOver);
328
329 if (clip == null)
330 {
331 Debug.LogError("audio clip not found " + voiceOver);
332
333 return;
335
336 PlayVoice(clip);
337 }
338
343 public void PlayVoice(AudioClip clip)
344 {
345 var soundId = PlayMusicWithParams(clip);
346
348 }
349
354 public void PlayRepeatable(string voiceOverName)
355 {
356 if (LastPlayedRepeatableAudio != null)
357 {
358 if (LastPlayedRepeatableAudio.Name == voiceOverName)
359 {
360 return;
361 }
362 }
364 var voiceOverAudioClip = voices.GetAudioClipVO(voiceOverName);
365
367 PlayRepeatable(voiceOverAudioClip, voiceOverName);
368 }
369
374 public void PlayOnceTime(string voiceOverName)
375 {
376 var voiceOverAudioClip = voices.GetAudioClipVO(voiceOverName);
377
378 PlayOnceTime(voiceOverAudioClip, voiceOverName);
379 }
380
386 public void UpdateLastRepeatable(string voiceOverName)
387 {
388 var voiceOverAudioClip = voices.GetAudioClipVO(voiceOverName);
389 var soundId = EazySoundManager.PlayMusic(voiceOverAudioClip, 0.001f, false, false, 0f, 0f);
390 var audio = EazySoundManager.GetAudio(soundId);
391
392 LastPlayedRepeatableAudio = new RepeatableAudio()
393 {
394 Name = voiceOverName,
395 Volume = 1f,
396 Audio = audio
397 };
398 }
399
406 public void PlayRepeatable(AudioClip clip, string voiceOverName)
407 {
408 if (clip == null)
409 {
410 Debug.LogError("No Audio File");
411 RepeatableAudioStopped?.Invoke();
412 return;
413 }
414
416 {
417 Name = voiceOverName,
418 Volume = 1f
419 };
420
421 var soundId = PlayMusicWithParams(clip);
422
423 LastPlayedRepeatableAudio.Audio = EazySoundManager.GetAudio(soundId);
424 }
425
431 public void PlayOnceTime(AudioClip clip, string voiceOverName)
432 {
433 if (clip == null)
434 {
435 Debug.LogError("No Audio File");
436 string idVO = LastPlayedRepeatableAudio.Name;
438 OnceTimeAudioStopped?.Invoke(idVO);
439 return;
441
443 {
444 Name = voiceOverName,
445 Volume = 1f
446 };
447
448 var soundId = PlayMusicWithParams(clip);
449
450 PlayOnceAudio.Audio = EazySoundManager.GetAudio(soundId);
451 }
452
453 private void OnAudioStopped(Hellmade.Sound.Audio audio)
454 {
455 if (LastPlayedRepeatableAudio == null) return;
456 if (PlayOnceAudio?.Audio == audio)
457 {
458 string idVO = LastPlayedRepeatableAudio.Name;
460 OnceTimeAudioStopped?.Invoke(idVO);
461 PlayOnceAudio.Name = null;
463 return;
464 }
465
466 if (LastPlayedRepeatableAudio.Audio != audio) return;
467
468 RepeatableAudioStopped?.Invoke();
469
470 if (_playRepeatableCoroutine != null)
471 {
472 StopCoroutine(_playRepeatableCoroutine);
473 }
474
476 }
477
481 public void PlayLastRepeatable()
482 {
483 if (_lastStartedVO != null && _lastStartedVO.IsPlaying)
484 {
485 _lastStartedVO.Stop();
486 }
487
491
492
497 public void StopRepeatable()
498 {
499 if (LastPlayedRepeatableAudio == null) return;
500
502
503 if (_playRepeatableCoroutine != null)
504 {
505 StopCoroutine(_playRepeatableCoroutine);
507
510 }
511
516 public void PauseRepeatable()
517 {
518 if (LastPlayedRepeatableAudio == null) return;
519
521
522 if (_playRepeatableCoroutine != null)
523 {
524 StopCoroutine(_playRepeatableCoroutine);
526
528 }
529
534 public void StopRepeatableTemporarily()
535 {
536 if (LastPlayedRepeatableAudio == null)
537 {
538 Debug.LogWarning("Repeatable sound is not exist");
539 return;
540 }
541
544 if (_playRepeatableCoroutine != null)
545 {
546 StopCoroutine(_playRepeatableCoroutine);
547 }
548
549 RepeatableAudioStopped?.Invoke();
550 }
551
555 public void StopOnceTimeTemporarily()
556 {
557 if (PlayOnceAudio == null)
558 {
559 Debug.LogWarning("PlayOnceAudio sound is not exist");
560 return;
561 }
562
563 PlayOnceAudio.Audio.Stop();
565 }
566
567 public void CleanOnceTimeVO()
568 {
569 if (PlayOnceAudio == null)
570 {
571 Debug.LogWarning("PlayOnceAudio sound is not exist");
572 return;
573 }
574
575 PlayOnceAudio = null;
577
581 public void StopAllVoiceOvers()
582 {
583 Debug.Log("TestAudio StopAllVoiceOvers");
585 }
586
590 public void StopAllFx()
591 {
592 Debug.Log("TestAudio StopAllFx");
594 }
595
600 public void SetRepeatableSoundVolume(float volume)
601 {
602 if (volume != _mutedVolume)
603 {
604 _isSoundMuted = false;
605 }
606
607 LastPlayedRepeatableAudio.Volume = volume;
608 if (PlayOnceAudio != null)
609 PlayOnceAudio.Volume = volume;
610
612 PlayOnceAudio?.Audio.SetVolume(PlayOnceAudio.Volume, 0, 0);
613 }
614
618 public void MuteRepeatableSound()
619 {
620 _isSoundMuted = true;
622 PlayOnceAudio?.Audio.SetVolume(_mutedVolume, 0, 0);
623 }
624
625 private IEnumerator PlayRepeatableCoroutine()
626 {
627 while (true)
628 {
629 yield return new WaitForSeconds(_soundRepeatDelay);
630
631 if (_lastStartedVO is { IsPlaying: true })
632 {
633 if (_playRepeatableCoroutine != null)
635 StopCoroutine(_playRepeatableCoroutine);
637 }
638 }
639 else
640 {
641 _lastStartedVO = null;
642 }
643
645 if (_isSoundMuted)
646 {
648 }
649 }
650 }
651
652 private int PlayMusicWithParams(AudioClip clip)
653 {
654 return EazySoundManager.PlayMusic(clip, 1f, false, false, 0f, 0f);
655 }
656 }
657
658 public class RepeatableAudio
659 {
660 public string Name { get; set; }
661 public float Volume { get; set; }
662 public Hellmade.Sound.Audio Audio { get; set; }
663 }
664}
The audio object.
Definition Audio.cs:10
void Stop()
Stop playing audio clip.
Definition Audio.cs:448
void SetVolume(float volume)
Sets the audio volume.
Definition Audio.cs:488
Static class responsible for playing and managing audio and sounds.
static int PlayMusic(AudioClip clip)
Play background music.
static void StopAllMusic()
Stop all music playing.
static int PlaySound(AudioClip clip, string name)
static Audio GetSoundAudio(int audioID)
Returns the sound fx Audio that has as its id the audioID if one is found, returns null if no such Au...
static Audio GetAudio(int audioID)
Returns the Audio that has as its id the audioID if one is found, returns null if no such Audio is fo...
static void StopAllSounds()
Stop all sound fx playing.
Main logic for playing fxs and voice overs.
int PlayMusicWithParams(AudioClip clip)
void PauseRepeatable()
Pause currently playing repeatable voice over and save its state Stop currently playing repeatable an...
void SetSfxVolume(string name, float volume)
Set sfx volume If the clip is missing from the data, it will return an warning.
int PlaySpatiallyFX(string fxName, float volume, bool loop, Transform sourceTransform)
Play 3d FX by fx name If the clip is missing from the data, it will return an error.
void StopRepeatable()
Stop currently playing repeatable voice over and remove its state Used for hard manual stop without n...
void StopPlaying()
Stop all fxs.
void InitCurrentVoiceOverdata(VoiceOverData voData)
Init current voiceover data.
void StopRepeatableTemporarily()
Temporarily stop currently playing repeatable voice over Stop currently playing repeatable and saving...
void UpdateLastRepeatable(string voiceOverName)
Update last repeatable Used to restore the last played audio.
void MuteRepeatableSound()
Mute repeatable sound volume.
SpatialSoundSettings defaultSpatialSoundSettings
[SerializeField]
RepeatableAudio PlayOnceAudio
Action< string > OnceTimeAudioStopped
void StopFxById(int fxId)
Stop fx by id If the clip is missing from the data, it will return an warning.
void StopOnceTimeTemporarily()
Stop currently playing voice over.
void PlayRepeatable(AudioClip clip, string voiceOverName)
Play repeatable voice over.
void PlayOnceTime(string voiceOverName)
Play once time by voice over name.
void OnAudioStopped(Hellmade.Sound.Audio audio)
void PlayOnceTime(AudioClip clip, string voiceOverName)
Play once time audio clip.
RepeatableAudio LastPlayedRepeatableAudio
bool IsPlaying(string fxName)
If the clip is missing from the data, it will return an error.
int PlayFX(string fxName)
Play FX by fx name.
void PlayLastRepeatable()
Play last played repeatable voice over from the moment it stopped.
void StopAllVoiceOvers()
Stop all voice overs.
void PlayOnlyOneFX(string fxName)
Play fx by name and stop the others that are playing.
SpatialSoundSettings GetSpatialSoundSettings(VoiceOverInfo voiceOverInfo)
void PlayVoice(AudioClip clip)
Play voice over audio clip.
void StopAllFx()
Stop all fxs.
void ConfigureAudioSource(Hellmade.Sound.Audio audio, SpatialSoundSettings spatialSoundSettings)
void PlayRepeatable(string voiceOverName)
Play voice repeating it at a certain time intervals.
VoiceOverInfo GetOverOverData(string voiceOverName)
Get voice over data Info by name If VO data is missing from the data, it will return an error.
void PlayFX(string fxName, bool loop)
Play FX by fx name If the clip is missing from the data, it will return an error.
void SetSfxPitch(int audioID, float pitch)
Set sfx pitch by audio id.
IEnumerator PlayRepeatableCoroutine()
void StopFx(string fxName)
Stop fx by name If the clip is missing from the data, it will return an error.
VoiceOverData soundsFx
[SerializeField]
void PlayFX(string fxName, float volume, bool loop)
Play FX by fx name If the clip is missing from the data, it will return an error.
VoiceOverData voices
[SerializeField]
Hellmade.Sound.Audio _lastStartedVO
void SetRepeatableSoundVolume(float volume)
Set repeatable sound volume.
void SetSfxVolume(int audioID, float volume)
Set fx volume by audio id.
void PlayVoice(string voiceOver)
Play voice over by id If the clip is missing from the data, it will return an error.
Hellmade.Sound.Audio Audio
Data that stores all information about voice overs.
VoiceOverInfo GetDataVO(string voID)
Get voiceover info by id.
AudioClip GetAudioClipVO(string voID)
Get voiceover audio clip by id.
[System.Serializable]
SpatialSoundSettings spatialSoundSettings