So i want the player to be able to choose music from his HardDrive to play during runtime.
I saw that there is an (Improved)File Browser but i’m not sure how to set it up.I tried setting it up but i was confused.
1.The browser consists of 3 C# scripts?
2.Where do i put those scripts?
3.Can i make it so the player chooses audio from his HardDrive?
I’m not familiar with C# so this is kinda hard for me Thanks.
so to get it to work you need the code sections from the Downloading to List section downwards. This will play any .ogg audio files in the specified directory. This line is the most important and the only line you may want to change. Set the musicDir variable to whatever you want the base music directory, also you way want to set the SearchOption to Recursive to go through each subdirectory of the specified folder. string[] playlist = Directory.GetFiles(@musicDir, "*.ogg", SearchOption.TopDirectoryOnly);
Here is my code from the stack overflow question, basically all you need to do from here is define the audio directory to find the music in (which you would use the file browser for). In order to use this script simply attach it to an empty game object within your scene.
It is also worth noting that Unity only supports certain file formats for music (google this). In the case of this code I am only looking for .ogg music files, you way wish to change that, also you may want a recursive search to look for music in sub-directories (check the Directory.GetFiles() documentation for more on that).
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
[RequireComponent(typeof(AudioSource))]
public class MP3AudioImporter : MonoBehaviour {
private List<AudioClip> audioClips;
private const string musicDir = "Path/To/Your/Music/Directory";
void Start()
{
audioClips = new List<AudioClip>();
StartCoroutine("PlayAudioList");
}
IEnumerator DownloadPlaylist()
{
string[] playlist = Directory.GetFiles(@musicDir, "*.ogg", SearchOption.TopDirectoryOnly);
foreach(string song in playlist)
{
WWW audioLoader = new WWW("file://" + song);
while( !audioLoader.isDone )
yield return null;
audioClips.Add(audioLoader.GetAudioClip(false));
}
}
IEnumerator PlayAudioList()
{
yield return StartCoroutine("DownloadPlaylist");
foreach(AudioClip song in audioClips)
{
audio.clip = song;
audio.Play();
yield return new WaitForSeconds(song.length + 1.0f);
}
}
}
I think we're getting there but(!): 1.The problem is i don't know how to set up the file browser ( xD ) 2.I get errors with your script error CS0116: A namespace can only contain types and namespace declarations I get this on (1,25),(2,16),(4,6),(10,13),(23,13) I wish i knew C#, it would be so much easier :(
Ok ive updated so you have my full code for audio, I would recommend using a hard coded Music directory path to begin with. Also you should go out and learn C# (or any object oriented language), it will come in handy for Unity. Writing out a full solution for you would kinda defeat the purpose of this space which is to get specific answers to problems (not full solutions)
also note that you need an AudioSource attached the the same empty Gameobject in order play the audioclips. Another thing is to make sure that the Audiosource sound type is set to a 2D sound, and change the rolloff to linear with a value of 1 (max volume at all times no matter your in game position)
Using the www class to load audio works with most formats, but does not allow GetData to be called on the created AudioClips. For your use, it should be fine - WWW.GetAudioClip.
Another solution, which I use to load wav samples into a sampler, is to parse the wav file, convert the int16s into floats, strip the header, create an AudioClip of the right size, and set the floats in it with SetData. It’s a bit of work, but I’ve posted some of the steps on the Unity Forums already.
I actually asked the same question a little while back, here is a link to mine, vote up the question and answer if its what you were after. [http://gamedev.stackexchange.com/questions/59525/issues-loading-in-audiosource-at-runtime-using-www-class/59527?noredirect=1#comment104789_59527][1] [1]: http://gamedev.stackexchange.com/questions/59525/issues-loading-in-audiosource-at-runtime-using-www-class/59527?noredirect=1#comment104789_59527
– chummscrubberDo i have to combine the "piecies" of code to make it work ?!By the way that is better than what i wanted :D
– iRaMb0so to get it to work you need the code sections from the Downloading to List section downwards. This will play any .ogg audio files in the specified directory. This line is the most important and the only line you may want to change. Set the musicDir variable to whatever you want the base music directory, also you way want to set the SearchOption to Recursive to go through each subdirectory of the specified folder. string[] playlist = Directory.GetFiles(@musicDir, "*.ogg", SearchOption.TopDirectoryOnly);
– chummscrubber