Okay here is my question. I want to load several sound files which are located in an external Music folder into a standalone player. I have searched the forums and Google and found some things which are helpful but my problem is in accessing my files from an array list. Below is one of my examples which I manage to get working but I also want to put in a next button which will move to the next songs as well as a random button which will shuffle which song it will play next. I manage to create something like this using resource load but I don’t want to set my player up that way. I want to give the user the ability to load their own songs.
Any help to this solution would be greatly appreciated. I have been trying to get this thing to work all week and I haven’t found a solution yet.
Code:
#pragma strict
var fileScroll : Vector2; // The variable to control where the scroll view 'looks' into its child elements.
@HideInInspector
var fileDirectory = Application.dataPath + "\\Music\\";
var loadedText : String = "";
var filePath : WWW;
var shuffle : boolean = false;
function Start () {
gameObject.AddComponent(AudioSource);
}
function Update () {
if (shuffle)
{
//code for shuffle song list?
}
}
function OnGUI () {
fileScroll = GUILayout.BeginScrollView (fileScroll, GUILayout.Width (400), GUILayout.Height (200));
//For all files in the data folder
for (var fileName : String in System.IO.Directory.GetFiles(fileDirectory))
{
//Only Wav, Ogg files count
if (fileName.EndsWith(".wav") || fileName.EndsWith(".ogg"))
{
//Get file names and assign them to a GUI button
if (GUILayout.Button(Path.GetFileNameWithoutExtension(fileName)))
{
// Get file path and assign them to the WWW component to be playable through audioclip
filePath = new WWW ("file://" + fileDirectory + Path.GetFileName(fileName));
//load the select song file to the GUI
loadedText = "Loaded: " + (Path.GetFileNameWithoutExtension(fileName));
}
}
}
GUILayout.EndScrollView();
GUI.Label(Rect(Screen.width/2 - 150, 0, 400, 50), loadedText); //display the selected song file onto GUI
if (GUI.Button(Rect(Screen.width/2 - 25, 200, 50, 30),"Play"))
{
SelectedSong();
//play = true;
}
if (GUI.Button(Rect(Screen.width/2 + 30, 200, 50, 30),"Next"))
{
//code needed to move to the next song
}
if (!shuffle GUI.Button (Rect (Screen.width / 2 - 40, 235, 80, 30), "Shuffle On"))
{
shuffle = true;
}
else if (shuffle GUI.Button (Rect (Screen.width / 2 - 40, 235, 80, 30), "Shuffle Off"))
{
shuffle = false;
}
}
function SelectedSong () {
for (var fileName : String in System.IO.Directory.GetFiles(fileDirectory))
{
audio.clip = filePath.GetAudioClip (false, true);
audio.Play();
}
}
I dont know if it helps, (or works) but this is a simple version of what you have.
#pragma strict
private var fileDirectory = Application.dataPath + "\\Music\\";
private var files:String[];
function Start(){
if(!audio)gameObject.AddComponent(AudioSource);
var arr:Array = new Array();
for (var fileName : String in System.IO.Directory.GetFiles(fileDirectory)){
if (fileName.EndsWith(".ogg")){
arr.push(fileName);
}
}
files = arr.ToBuiltin(String) as String[];
}
function Update(){
if(!files) return;
if(audio.clip audio.clip.isReadyToPlay) audio.Play();
}
function OnGUI(){
// do your gui stuff here
for(var i=0; i<files.length; i++){
if (GUI.Button(Rect(10,10 + i * 35,150,30), files[i])) LoadAudioFile(files[i]);
}
}
function LoadAudioFile(url){
audio.Stop();
var www:WWW = new WWW (url);
audio.clip = www.GetAudioClip(false, true);
}
Thanks Bigmisturb for the reply. I actually was coming up with a similar solution to what you sent me. If I come up with a solution I’ll post what I come up with. I manage to get it to shuffle through the music list but its not switching the audio files.
Thanks for the help Bigmisturb this is what I manage to come up with from what you sent me. Now I can play each song or hit next or previous to play the before or after track. now I’m going to add some more features to it.
#pragma strict
var fileScroll : Vector2; // The variable to control where the scrollview 'looks' into its child elements.
@HideInInspector
var fileDirectory = Application.dataPath + "\\Music\\";
var files : String[];
var songs : String[];
var sel : int = 0;
var loadedText : String;
private var shuffle : boolean = false;
function Start(){
if (!audio)gameObject.AddComponent(AudioSource);
var arr : Array = new Array();
var att : Array = new Array();
for (var fileName : String in System.IO.Directory.GetFiles(fileDirectory))
{
if (fileName.EndsWith(".wav") || fileName.EndsWith(".ogg"))
{
arr.push(fileName); //sends out the file name and its directory location
att.push(Path.GetFileNameWithoutExtension(fileName)); //sends out only the file name.
}
}
files = arr.ToBuiltin(String) as String[];
songs = att.ToBuiltin(String) as String[];
}
function Update() {
if (!files)
return;
if (sel == files.length)
{
sel = 0;
}
//i == sel;
}
function OnGUI() {
// do your gui stuff here
for (var i = 0; i < files.length; i++)
{
if (GUI.Button(Rect(10, 10 + i * 35, 150, 30), files[i]))
{
LoadAudioFile(files[i]);
}
}
GUI.Label(Rect(Screen.width/2 - 150, 0, 400, 50), loadedText);
if (GUI.Button(Rect(Screen.width/2 - 25, 200, 50, 30),"Play"))
{
LoadAudioFile(files[sel]);
}
if (GUI.Button(Rect(Screen.width/2 + 30, 200, 50, 30),"Next"))
{
sel++;
LoadAudioFile(files[sel]);
}
if (GUI.Button(Rect(Screen.width/2 - 80, 200, 50, 30),"Prev"))
{
sel--;
LoadAudioFile(files[sel]);
}
}
function LoadAudioFile(fileDirectory) {
loadedText = "Loaded: " + (songs[sel]); //display the selected song text
var filePath : WWW = new WWW ("file://" + fileDirectory);
audio.clip = filePath.GetAudioClip(false, true);
yield;
audio.Play();
}