Liberi
An exergame built for kids with CP!
MusicPlayer.cs
1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 
9 public class MusicPlayer : MonoBehaviour
10 {
11  static MusicPlayer _instance;
12 
13  public static AudioSource ActiveSource
14  {
15  get
16  {
17  return _instance._sources[_instance._activeSourceIndex];
18  }
19  }
20 
21  public static AudioSource IdleSource
22  {
23  get
24  {
25  int idleIndex = _instance._activeSourceIndex == 0 ? 1 : 0;
26  return _instance._sources[idleIndex];
27  }
28  }
29 
30  public AnimationCurve FadeCurve;
31 
32  AudioSource[] _sources;
33  int _activeSourceIndex;
34 
35  void Awake ()
36  {
37  _instance = this;
38  _sources = GetComponents<AudioSource>();
39  _activeSourceIndex = 0;
40  }
41 
42  public static void Play (string trackName)
43  {
44  AudioClip track = Resources.Load<AudioClip>(trackName);
45  Play(track);
46  }
47 
48  public static void Play (AudioClip track)
49  {
50  ActiveSource.loop = true;
51  ActiveSource.clip = track;
52  ActiveSource.volume = 1;
53  ActiveSource.Play();
54  }
55 
56  public static void PlayOneShot (string trackName)
57  {
58  AudioClip track = Resources.Load<AudioClip>(trackName);
59  PlayOneShot(track);
60  }
61 
65  public static void PlayOneShot (AudioClip track)
66  {
67  ActiveSource.loop = false;
68  ActiveSource.Play(track);
69  }
70 
74  public static void Stop ()
75  {
76  ActiveSource.Stop();
77  }
78 
82  public static void CrossFade (string trackName, float fadeTime = 1.0f)
83  {
84  AudioClip track = Resources.Load<AudioClip>(trackName);
85  CrossFade(track, fadeTime);
86  }
87 
91  public static void CrossFade (AudioClip track, float fadeTime = 1.0f)
92  {
93  _instance.StartCoroutine(_instance.CrossFadeAsync(track, fadeTime));
94  }
95 
99  public static void FadeOut (float fadeTime = 1.0f)
100  {
101  _instance.StartCoroutine(_instance.CrossFadeAsync(null, fadeTime));
102  }
103 
104  void ChangeSource ()
105  {
106  _activeSourceIndex = _activeSourceIndex == 0 ? 1 : 0;
107  }
108 
109  IEnumerator CrossFadeAsync (AudioClip incomingTrack, float fadeTime)
110  {
111  IdleSource.clip = incomingTrack;
112  IdleSource.volume = 0;
113 
114  if (IdleSource.clip != null)
115  IdleSource.Play();
116 
117  for (float t = 0; t < fadeTime; t += Time.deltaTime)
118  {
119  float s = t/fadeTime;
120  ActiveSource.volume = FadeCurve.Evaluate(1 - s);
121  IdleSource.volume = FadeCurve.Evaluate(s);
122  yield return null;
123  }
124 
125  ChangeSource();
126  ActiveSource.loop = true;
127  IdleSource.Stop();
128  IdleSource.clip = null;
129  }
130 }
static void PlayOneShot(AudioClip track)
Abruptly play an audio track and don't loop it.
Definition: MusicPlayer.cs:65
A Singleton Class that manages the music playing. Presists across scenes and provides various methods...
Definition: MusicPlayer.cs:9
static void CrossFade(AudioClip track, float fadeTime=1.0f)
Given an audio clip, crossfade from the current track to it.
Definition: MusicPlayer.cs:91
static void CrossFade(string trackName, float fadeTime=1.0f)
Given a string name of a music track, load that track and crossfade from the current track to it...
Definition: MusicPlayer.cs:82
static void Stop()
Abruptly Stop playing the current audio track.
Definition: MusicPlayer.cs:74
static void FadeOut(float fadeTime=1.0f)
Fade Out the currently playing track.
Definition: MusicPlayer.cs:99