Importing audio files at runtime

Hello! I made a topic about this before, but after weeks of bumping and looking for the answer I came up empty-handed. I hope this does not happen again because this is getting really important. :slight_smile:

So my team and I are working on a game like AudioSurf which reads audio files and does things according to the audio output. Everything’s good except that it can only read files that have been imported in the editor before. We need to enable the players to pick their own audio files from their computers and play them.

We had several approaches for this but none of them worked. I am aware that Unity can only read ogg files but even with ogg files we failed to make this feature.

I am not going to post code this time because I would like to start from the zero and collect more information about how such mechanics would work.

Can anybody point me in the right direction ? Any help will be really, really appreciated!

Cheers!

I believe that all of Unity’s audio import stuff is only present in the editor, not at runtime, so you’ll have to use third-party libraries to load the audio files and decompress them yourself.

Once you’ve got the raw audio data, you can use SetData to put it into an AudioClip for playback.

I’ll do anything to be able to import audio at runtime so there’s no problem for me to use third-party libraries. However I have no clue what to search for. Can you point me what exactly to search for ?

Oh no, this wouldn’t work. I’ve tried it many times and I never got it to work. I’ve tried both the oggVorbis variable and the GetAudioClip function. Here’s my previous topic: http://forum.unity3d.com/threads/134361-Does-not-load-the-audio-clip.

Thanks for the answers so far! :slight_smile:

What audio file formats do you need to support?

Basically, look into standard .NET (i.e. non-Unity) libraries and APIs for loading and working with audio files.

Well we need the players to be able to load the most common files - mp3, wav and ogg (at least to my knowledge).

I’ve been looking up the .NET framework class library trying to find something audio related but I couldn’t find such thing. I don’t have a lot of experience in dealing with external libraries and finding ones yet so please excuse my ignorance. My problem is that I don’t really know what to look for so I will greatly appreciate it if you point me into the right direction!

Thanks for the help so far! :slight_smile:

http://www.robburke.net/mle/mp3sharp/

1 Like

Oh sweet! Thank you (how did you find this)!

I’m not at work so currently I’m not able to start working on it, but just to make things clear:
I open an audio file, use one of the decoder classes (don’t know which one because I haven’t found any documentation yet) in the decoder folder to read the file and turn it into an array of floats, then I use AudioClip.SetData() to import the information into an audio clip and play it, right ? If it’s like that, that should be fairly easy to achieve. I hope it doesn’t take too long for the process to read the file and convert it to an AudioClip.

I just googled “decode MP3 C#.”

Yeah, that’s pretty much the procedure. Note that there is a second approach for ‘streaming’ the audio in, which may be more appropriate for music: instead of decoding the entire MP3 in one go and pushing its data into the AudioClip, when you create the AudioClip you can specify callback functions that only have to provide the next ‘block’ of data. So you could gradually decode the MP3 file, block-by-block (maybe on another thread) and just submit the decoded data to Unity via those callbacks when necessary.

I’ll stick to the first method for now. In our case we need to have an array of audio clips which need to be accessed fast so maybe I’ll just put a loading screen at the begining while the whole process takes place.

Anyway, I starded to import the library into Unity but I ran into an issue. The console said I was missing assembly references to some namespaces (system.data, system.drawing, system.windows) so I started to search for those files in the net and found System.Data and System.Drawing and imported them into Unity. Then I found System.Windows.dll in Silverlight’s directory on the computer and also imported it.All the assembly references disappeared but left me with another error:

I’ve never seen such an error before. I searched for it and I couldn’t find a solution that works. It seems to me as something’s incompatible. I switched from .NET Framework 2.0 Subset to .NET Framework 2.0 and still this error doesn’t budge.

Have you seen this before and do you know what causes it ?

You have to make sure that all external class libraries are built against version 2.0, 3.0 or 3.5 of the .NET framework. The errors you’re seeing are probably because you’re trying to load something build against .NET 4.0.

With the use of WWW class u can read external audio (I tried .wav .ogg) file in unity3d. Its works fine. For playing external audio file u dont need to build ur project again. Just replace ur audio file at ur datapath.

So you’re saying that either one of the dll files I’ve imported is built agains .NET 4.0 ? How can I know which one is it and where can I find one built against 2.0 ? If I’ve understood you correct, I think it might be the System.Windows dll which I got from my computer (I have .NET 4.0 installed) however I couldn’t find this speciffic library anywhere unlike the other two.

Thanks for joining Ram Thakkar :slight_smile:
I gave a link to my previous topic about this above:
http://forum.unity3d.com/threads/134361-Does-not-load-the-audio-clip.. I couldn’t get this to work after weeks of trying…it just didn’t load the audio clip into the audio source. After lots of debugging I couldn’t find the reason why it wasn’t working. Maybe you can tell me what I did wrong ? :slight_smile:

1 Like

Hello Dark protocol,

With the below code i successfully load external audio into my unity exe:

var www = new WWW (“file://” + Application.dataPath.Substring (0, Application.dataPath.LastIndexOf (“/”)) + “/result.wav”);

AudioClip myAudioClip= www.audioClip;
while (!myAudioClip.isReadyToPlay)
yield return www;
gameObject.GetComponent ().audio.clip = myAudioClip;

audio.Play ();

2 Likes

you just forgot to write “file://” before ur Application.datapath or may be u dont know about this.

Yes, I tried your method in Javascript and it worked for me. It’s a bummer that I can’t stream several files, put them in an array, change the scene and still have them as playable audio clips (because that’s what we need in the game) but I can keep an array with file links and as soon a the new scene starts I can start loading them one by one while playing the first loaded one.

Well okay, that’s awesome so far and many thanks for the help, Ram Thakkar :slight_smile:

I’m currently having difficulties finding out how to use the decoder classes from mp3sharp. I couldn’t find any documentation on it so I guess I’ll just have to read through all the classes and find out how they work.

But in mycase, i played multiple audio files from external source with same method and its work fine for me.

It was my bad. There was a problem with something else that prevented the whole thing from working. It works as a charm now after I fxed it.
I’m having another issue now. I need the imported songs to play in 2D, not in 3D so I wrote something like this:

var samples = new float[myAudioClip.samples * myAudioClip.channels];
        			myAudioClip.GetData(samples, 0);
				var newClip : AudioClip = AudioClip.Create("song", myAudioClip.samples, myAudioClip.channels, myAudioClip.frequency, false, true);
					newClip.SetData(samples, 0);
   					songList.Add(myAudioClip);

This is supposed to create a new audio clip which is 2D, extract the data from the imported one and put it into the new one. However, after I play the new clip, I don’t hear anything. The audio clip is playing but it has no sound information in itself. I couldn’t find other topics for this so I need to ask for this too. What could be the cause of this ?

Thanks both of you for the help. It is much appreciated!

I got the error. The console says “Cannot get data from streamed sample”. So does that mean that there isn’t any way to get the samples? That would be bad :frowning:

1 Like