- Home /
Loading and playing AudioClip at runtime
Hello, I want the user to select a music file from his computer and play it in the game. Once I have the file path, I am unable to get the file to play. Here is what I have tried:
function Start () {
StartMusic();
}
function StartMusic() : IEnumerable{
var importedClip : AudioClip;
var pathUrl : WWW = new WWW("file://C:/1.ogg");
importedClip = pathUrl.GetAudioClip(false);
while(!importedClip.isReadyToPlay){
yield pathUrl;
}
audio.clip = importedClip;
audio.Play();
}
I tried both wav and ogg files (mp3 is not supported). I don't get any error messages, but the music never starts! Is there anything I should/should not be doing here? thx!
Edit: I have tried with an imported audio clip and it works just fine, the problem seems to be with the usage of WWW.
Have you tried an audio clip that was imported to see if it'll play? That will help deter$$anonymous$$e whether it's an import issue, an issue with the AudioSource, or a general logic issue.
Have you tried putting some break points in and debugging? Does it ever get past the loop waiting on isReadyToPlay? If it does get past there what's importedClip looks like if you inspect it? Do the values in its fields make sense?
By the way, Unity can only use .ogg files. EDIT: Perhaps it gets played but you can't hear it. Try changing pathUrl.GetAudioClip(true); to pathUrl.GetAudioClip(false);
Answer by dave_mm0 · Apr 25, 2013 at 02:16 PM
So, since you can't stream mp3s with WWW on a Desktop (only on mobile) and on iOS you can't access mp3s with WWW because of the sandbox... I ended up reading the MP3 (native iOS, Android code with Unity plugin, and MP3Sharp on Desktop) and using the PCMCallBack on the SoundClip to import at runtime.
What plug-in did you use for Android?
Can you detail how you were able to successfully import and play an $$anonymous$$P3 file across all 3 platforms?
For Android & PC: I am reading the PC$$anonymous$$ data using F$$anonymous$$od and strea$$anonymous$$g it to Unity3d. I wrote the plugin to do this myself. For iOS: I am reading the PC$$anonymous$$ data using the AV$$anonymous$$ediaFoundation framework, and strea$$anonymous$$g the data to Unity in the same way
Thank you very much for the reply dave!
Forgive my ignorance, I'm quite experienced with Unity but very new to working with $$anonymous$$P3 files:
What is PC$$anonymous$$ data? Is your homebrew plug-in available somewhere like GitHub where read through it?
I looked at F$$anonymous$$od and AV$$anonymous$$ediaFoundation - did you use a commercial license for F$$anonymous$$od?
Thanks for your time dave!
PC$$anonymous$$ Data is the raw audio data you get after uncompressing an $$anonymous$$P3 file, it stands for Pulse-code modulation. You can use Unitys AudioClip.Create and ask your plugin for the data in the PC$$anonymous$$ReaderCallback function, and copy the PC$$anonymous$$ data into your audioclip. $$anonymous$$y plugin is not available, since I made it for a commercial game which should come out soon :) I did use a commercial license of F$$anonymous$$od, but since Unity already comes with F$$anonymous$$od, if you contact the nice people there, you can work out a deal to use it for cheap.
Thanks very much dave, I appreciate the replies! That cleared a lot up for me. Thanks for the tip too! Unfortunately I'm a 4th year college student and can't afford to get the Unity Pro license to use plug-ins (or pay F$$anonymous$$od). I think I'll just implement a complete solution for iOS/Droid with WWW and a gimped one for PC.
Answer by aeroson · Aug 18, 2015 at 06:08 PM
Just released asset that allows you to load AudioClip from waw, mp3, aiff and ogg files at runtime: http://u3d.as/content/neitri-industries/runtime-audio-clip-loader
Your answer
Follow this Question
Related Questions
Finding size of images imported at runtime 2 Answers
php WWW not working in unity web player 1 Answer
www faster than Resources.Load? 0 Answers
Load object at runtime using webplayer 0 Answers
@ makes problem in ios 1 Answer