-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMusicPlayer.cs
131 lines (103 loc) · 4.2 KB
/
MusicPlayer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// The improved music player system.
// Previous system did not work well with the rest of the code, so I just re-made it.
// Please dont touch it if you dont need to change anything. Audio is a fragile thing.
public class MusicPlayer : MonoBehaviour
{
[Header("Audio References")] // Audio Source & Clip - THIS REQUIRES THE EXISTANCE OF BOTH SOURCES IN ORDER TO WORK!
[SerializeField]
private AudioSource _ASPrimary; // Primary Audio Source
[SerializeField]
private AudioSource _ASSecondary; // Secondary Audio Source
[SerializeField]
private AudioClip[] _Aclips; // List of audio clips (Any amount)
[Header("Audio Settings")] // Audio Settings
public float _AudioVolume = 1f; // Should stay at 1f, but does not matter that much.
[SerializeField]
private float _AudioFadeTime = 15f; // How long before it starts to fade - Should be a long value (25 - 5)
[Header("Debug Settings")]
[SerializeField]
private bool _IsDebugFadeEnabled = false; // Enable to debug fading
// Storage
private int _FadeState = 0; // Controls fading status
private int _CurrentValue;
private int _TempRandomValue; // Used for checking if a song has already played previously.
void Start()
{
// Firt time bootup
_CurrentValue = Random.Range (0, _Aclips.Length);
_ASPrimary.clip = _Aclips[_CurrentValue];
_ASPrimary.Play(); // THANK YOU JAPAN! Had it not been for the japanese, I would not have known that PlayOneShot does not store clip data.
_ASPrimary.volume = _AudioVolume;
_ASSecondary.volume = 0f;
Invoke("S1E", _ASPrimary.clip.length - _AudioFadeTime);
}
void Update()
{
if (_FadeState == 1)
{
_ASPrimary.volume = Mathf.Lerp(_ASPrimary.volume, 0, Time.deltaTime / _AudioFadeTime);
_ASSecondary.volume = Mathf.Lerp(_ASSecondary.volume, _AudioVolume, Time.deltaTime / _AudioFadeTime);
}
if (_FadeState == 2)
{
_ASPrimary.volume = Mathf.Lerp(_ASPrimary.volume, _AudioVolume, Time.deltaTime / _AudioFadeTime);
_ASSecondary.volume = Mathf.Lerp(_ASSecondary.volume, 0, Time.deltaTime / _AudioFadeTime);
}
if (_IsDebugFadeEnabled == true && _FadeState != 0)
{
Debug.Log(_ASPrimary.volume);
}
}
void SetNewSong() // Ensures no song is played twice in a row.
{
_TempRandomValue = Random.Range (0, _Aclips.Length);
while (_TempRandomValue == _CurrentValue)
{
_TempRandomValue = Random.Range (0, _Aclips.Length);
}
_CurrentValue = _TempRandomValue;
}
// HERE BE DRAGONS! These functions are hard to follow, tread carefuly.
// Think of this as a series of logic gates instead of functions.
void S1E()
{
Debug.Log("MUSIC PLAYER LOG: S1E");
SetNewSong();
_ASSecondary.clip = _Aclips[_CurrentValue];
_ASSecondary.Play();
_ASSecondary.volume = 0f;
_FadeState = 1;
// This exectured even though it had an if statement, so I had to manually disable it until I find a workaround.
//if (_IsDebugFadeEnabled == true){Debug.Break();} // Pause for automated testing - Ignore.
Invoke("S1ES", _AudioFadeTime);
}
void S1ES()
{
Debug.Log("MUSIC PLAYER LOG: S1ES");
_ASPrimary.Stop();
_FadeState = 0;
_ASSecondary.volume = _AudioVolume;
Invoke("S2E", _ASSecondary.clip.length - _AudioFadeTime);
}
void S2E()
{
Debug.Log("MUSIC PLAYER LOG: S2E");
SetNewSong();
_ASSecondary.clip = _Aclips[_CurrentValue];
_ASPrimary.Play();
_ASPrimary.volume = 0f;
_FadeState = 2;
Invoke("S2ES", _AudioFadeTime);
}
void S2ES()
{
Debug.Log("MUSIC PLAYER LOG: S2ES");
_ASSecondary.Stop();
_FadeState = 0;
_ASPrimary.volume = _AudioVolume;
Invoke("S1E", _ASPrimary.clip.length - _AudioFadeTime);
}
}