- Home /
Streaming music from a folder or file
Currently I am developing a racing game, the gimmick of this game is the players ability to customize practically everything, from your car to your music. Hence, as you have probably figured out, the player needs to be able to play their own music. Currently I am using the following code, however when I debug, have the music all mapped and all, it simply doesn't play.
Code for setting the music using UnityEngine; using System.Collections; using System.IO;
public class OptionsMenu : MonoBehaviour {
public bool MenuState = false;
public MainMenu main;
public GUISkin DatGUI;
string MLocation = string.Empty;
void OnGUI()
{ if (MenuState)
{
GUI.skin = DatGUI;
//Allows the player to enter the music list location
GUI.Box(new Rect(0, 40, Screen.width, 60),
"This is the music list location. Note that forward slashes should be used, not backslashes. Note that all music should be in OGG or WAV format. Format factory is recommended if conversion needed.");
MLocation = GUI.TextField(new Rect(0,100, Screen.width, 40),MLocation);
// after the player clicks the following button, the game does a whole bunch of complex stuff. Essentially it makes a list of all the music available before setting the length of this list.
// NOTE TO SELF!!! This is without the file:/// appended to the start. When importing through WWW that file:/// needs to be added to the start
if (GUI.Button(new Rect(0,140, Screen.width, 40), "Set directory"))
{
PlayerPrefs.SetString("MusicLocation",MLocation);
DirectoryInfo info = new DirectoryInfo(MLocation);
FileInfo[] fileinfo = info.GetFiles();
// The above code: sets the files music location, then gets a list of files within the location
int FileListLength = 0;
foreach (FileInfo file in fileinfo)
{
if (file.Extension == ".ogg" || file.Extension == ".wav")
{
PlayerPrefs.SetString(FileListLength.ToString(), file.Name);
FileListLength++;
}
}
PlayerPrefs.SetInt("MusicList", FileListLength);
//this goes through each file, validating that it can be used.
}
GUI.Box(new Rect(0, 180, Screen.width, 40), "Playlist location: " + PlayerPrefs.GetString("MusicLocation") + " Playlist size: " + PlayerPrefs.GetInt("MusicList") + "Example Track: " + PlayerPrefs.GetString("1"));
if (GUI.Button(new Rect(0, 0, Screen.width, 40), "BACK"))
{
MenuState = false;
main.MenuState = true;
}
}
}
}
Code for playing the music private int ChosenTrack; // Once per frame update is called. This tells the game to take user input and tells the AI to respond to the world around it. void Update() {
UserInput();
foreach (Car CurrentCar in CarList)
{
AIInput.AIinput(CurrentCar);
}
}
// this sets the players music so that it starts playing
void Start()
{
ChosenTrack = (int)(UnityEngine.Random.value * PlayerPrefs.GetInt("MusicList"));
string str = PlayerPrefs.GetString(ChosenTrack.ToString());
WWW www = new WWW("file:///" + PlayerPrefs.GetString("MusicLocation") + PlayerPrefs.GetString(ChosenTrack.ToString()));
audio.clip = www.GetAudioClip(false,false);
}
you might need to yield on www. It probly wont return the file instantly, even though its local
Your answer
Follow this Question
Related Questions
Can I change the folder that my lightmap .exr files are placed? 1 Answer
File.Create not working 0 Answers
How do I find a folder I can write data to on Android? 3 Answers
Music play when MOVING.. 2 Answers