Skip to content

Commit 15003fa

Browse files
authored
Update BetterMusicPlayer.cs
1 parent 80dfd2d commit 15003fa

File tree

1 file changed

+95
-109
lines changed

1 file changed

+95
-109
lines changed

BetterMusicPlayer.cs

Lines changed: 95 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -2,129 +2,115 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text;
5-
using UnityEngine;
65
using PiTung.Console;
76

87
namespace CustomMusic {
9-
public class BetterMusicPlayer : MonoBehaviour {
10-
List<Track> playlist;
11-
static GameObject boombox;
12-
static GameObject player;
13-
float secondsBetweenTracks = 200f;
14-
int currentSongId = -1;
15-
bool paused = false;
8+
public class CommandPlay : Command {
9+
public override string Name => "play";
10+
public override string Usage => $"{Name} (#)";
11+
public override string Description => "Forces a track to play. Leave # blank for random, use # to pick a track.";
1612

17-
public static BetterMusicPlayer CreateSelf() {
18-
player=GameObject.Find("FirstPersonCharacter");
19-
boombox=new GameObject();boombox.transform.SetParent(player.transform);
20-
boombox.AddComponent<AudioSource>().spatialBlend=0.0f;
21-
return player.AddComponent<BetterMusicPlayer>();
22-
}
23-
24-
void Start() {
25-
boombox.GetComponent<AudioSource>().spatialBlend=0.0f;
13+
public override bool Execute(IEnumerable<string> args) {
14+
if(args.Count()==0)
15+
CustomMusic.bmp.PlayRandom();
16+
else {
17+
int result;
18+
if(int.TryParse(args.ElementAt(0), out result))
19+
CustomMusic.bmp.Play(result);
20+
else return false;
21+
}
22+
23+
return true;
2624
}
25+
}
26+
public class CommandBeginPlaylist : Command {
27+
public override string Name => "playlist";
28+
public override string Usage => $"{Name} (random/#)";
29+
public override string Description => "Starts playing tracks in series. Put random to shuffle the music, or a number to start at that track.";
2730

28-
public BetterMusicPlayer() {
29-
playlist=new List<Track>();
30-
}
31-
public void AddNewTrack(string trackName, AudioClip ac) {
32-
paused=false;
33-
playlist.Add(new Track(trackName,ac));
34-
}
35-
public void SetTimeBetweenTracks(float newTime) {
36-
secondsBetweenTracks=newTime;
37-
}
38-
public void Play(int songId) {
39-
paused=false;
40-
StopTrack();
41-
playlist[songId].Play(boombox.GetComponent<AudioSource>());
42-
}
43-
public void PlayRandom() {
44-
paused=false;
45-
StopTrack();
46-
int num = UnityEngine.Random.Range(0, playlist.Count());
47-
playlist[num].Play(boombox.GetComponent<AudioSource>());
48-
currentSongId=num;
49-
}
50-
private void SetCurrentSongId(int SongId) {
51-
currentSongId=SongId;
52-
}
53-
public void BeginPlaylist() {
54-
paused=false;
55-
SetCurrentSongId(-1);
56-
PlayNext();
57-
StartCoroutine(Playlist_Normal());
58-
}
59-
public void BeginPlaylistAt(int start) {
60-
paused=false;
61-
SetCurrentSongId(start);
62-
PlayNext();
63-
StartCoroutine(Playlist_Normal());
64-
}
65-
public void BeginPlaylist_Shuffle() {
66-
paused=false;
67-
PlayRandom();
68-
StartCoroutine(Playlist_Shuffle());
69-
}
70-
private IEnumerator<object> Playlist_Normal() {
71-
yield return (object) new WaitForSecondsRealtime(secondsBetweenTracks);
72-
PlayNext();
73-
AudioSource audsrc = boombox.GetComponent<AudioSource>();
74-
yield return (object) new WaitUntil(() => audsrc.time>=audsrc.clip.length);
75-
}
76-
private IEnumerator<object> Playlist_Shuffle() {
77-
yield return (object) new WaitForSecondsRealtime(secondsBetweenTracks);
78-
PlayRandom();
79-
AudioSource audsrc = boombox.GetComponent<AudioSource>();
80-
yield return (object) new WaitUntil(() => audsrc.time >= audsrc.clip.length);
81-
}
82-
public void PlayNext() {
83-
paused=false;
84-
currentSongId++;
85-
if(currentSongId>=playlist.Count()) currentSongId=0;
86-
Play(currentSongId);
87-
}
88-
public void PlayLast() {
89-
paused=false;
90-
currentSongId--;
91-
if(currentSongId<0) currentSongId=playlist.Count()-1;
92-
Play(currentSongId);
93-
}
94-
public void StopTrack() {
95-
foreach(AudioSource a in GameObject.FindObjectsOfType<AudioSource>()) {
96-
a.Stop();
31+
public override bool Execute(IEnumerable<string> args) {
32+
if(args.Count()==0)
33+
CustomMusic.bmp.BeginPlaylist();
34+
else if(args.ElementAt(0)=="random")
35+
CustomMusic.bmp.BeginPlaylist_Shuffle();
36+
else {
37+
int result;
38+
if(int.TryParse(args.ElementAt(0), out result))
39+
CustomMusic.bmp.BeginPlaylistAt(result);
40+
else return false;
9741
}
42+
return true;
9843
}
99-
public string GetListOfSongs() {
100-
string retVal = "";
101-
for(int i = 0; i < playlist.Count(); i++) {
102-
retVal+=i+": "+playlist[i].trackName+"\n";
103-
}
104-
return retVal;
44+
}
45+
public class CommandStopPlaying : Command {
46+
public override string Name => "stopmusic";
47+
public override string Usage => $"{Name}";
48+
public override string Description => "Forces all music to stop.";
49+
50+
public override bool Execute(IEnumerable<string> args) {
51+
if(args.Count()==0)
52+
CustomMusic.bmp.StopTrack();
53+
else return false;
54+
return true;
10555
}
106-
public string GetCurrentSong() {
107-
return currentSongId+": "+playlist[currentSongId].trackName;
56+
}
57+
public class CommandViewTracks : Command {
58+
public override string Name => "listtracks";
59+
public override string Usage => $"{Name}";
60+
public override string Description => "Lists all tracks and IDs.";
61+
62+
public override bool Execute(IEnumerable<string> args) {
63+
if(args.Count()==0)
64+
IGConsole.Log(CustomMusic.bmp.GetListOfSongs());
65+
else return false;
66+
return true;
10867
}
109-
public void TogglePause() {
110-
paused=!paused;
111-
if(paused) boombox.GetComponent<AudioSource>().Pause();
112-
else boombox.GetComponent<AudioSource>().UnPause();
68+
}
69+
public class CommandNextSong : Command {
70+
public override string Name => "nextsong";
71+
public override string Usage => $"{Name}";
72+
public override string Description => "Skips to the next track.";
73+
74+
public override bool Execute(IEnumerable<string> args) {
75+
if(args.Count()==0)
76+
CustomMusic.bmp.PlayNext();
77+
else return false;
78+
return true;
11379
}
11480
}
115-
class Track : MonoBehaviour {
116-
public string trackName;
117-
AudioClip ac;
118-
public Track(string trackName, AudioClip ac) {
119-
this.trackName=trackName;this.ac=ac;
81+
public class CommandLastSong : Command {
82+
public override string Name => "lastsong";
83+
public override string Usage => $"{Name}";
84+
public override string Description => "Goes back one track.";
85+
86+
public override bool Execute(IEnumerable<string> args) {
87+
if(args.Count()==0)
88+
CustomMusic.bmp.PlayLast();
89+
else return false;
90+
return true;
12091
}
121-
public void Play(AudioSource audSrc) {
122-
IGConsole.Log("Now playing... "+trackName);
123-
audSrc.clip=ac;
124-
audSrc.Play();
92+
}
93+
public class CommandCurrentSong : Command {
94+
public override string Name => "whatsplaying";
95+
public override string Usage => $"{Name}";
96+
public override string Description => "View the track currently playing.";
97+
98+
public override bool Execute(IEnumerable<string> args) {
99+
if(args.Count()==0)
100+
IGConsole.Log(CustomMusic.bmp.GetCurrentSong());
101+
else return false;
102+
return true;
125103
}
126-
public float GetTrackLength() {
127-
return ac.length;
104+
}
105+
public class CommandPauseMusic : Command {
106+
public override string Name => "togglepause";
107+
public override string Usage => $"{Name}";
108+
public override string Description => "Toggle the current track between paused/unpaused.";
109+
public override bool Execute(IEnumerable<string> args) {
110+
if(args.Count()==0)
111+
CustomMusic.bmp.TogglePause();
112+
else return false;
113+
return true;
128114
}
129115
}
130116
}

0 commit comments

Comments
 (0)