- Home /
Play an AudioSource
Hi guys, i'm working on a script that register save and play audio from microphone.
I did almost all the only thing that i've to fix is the audio.Play();
this is my code:
private var buttonWidth = Screen.width * 0.2;
private var buttonHeight = Screen.width * 0.05;
private var buttonX = Screen.width *0.01;
private var buttonY = Screen.width *0.01;
private var micIn : boolean = false;
private var getAudioSource : AudioClip;
private var deviceName : String;
function Start() {
if(Microphone.devices.Length <= 0){
Debug.Log("Microphone not Connected");
}else{
micIn = true;
for(var i : int = 0; i < Microphone.devices.length; i++){
Debug.Log(Microphone.devices[i]);
deviceName = Microphone.devices[i];
}
}
}
function registerAudio() {
Debug.Log("Recording Audio");
getAudioSource = Microphone.Start(deviceName , false, 10, 44100);
}
function stopRecording(){
Debug.Log("Stop Recording");
Microphone.End(deviceName);
}
function saveAudio(){
SavWav.Save("registerAudio", getAudioSource);
}
function playAudio(){
getAudioSource.Play();
}
function OnGUI(){
if(micIn){
if(!Microphone.IsRecording(deviceName)){
if(GUI.Button(Rect(buttonX,buttonY, buttonWidth, buttonHeight), "Record")){
registerAudio();
}
}else{
if(GUI.Button(Rect(buttonX, buttonY, buttonWidth, buttonHeight), "Stop Recording")){
stopRecording();
}
}
if(GUI.Button(Rect(buttonX,buttonY+(buttonHeight*1.2), buttonWidth, buttonHeight), "PlayAudio")){
playAudio();
}
if(GUI.Button(Rect(buttonX, buttonY+((buttonHeight*2)*1.2), buttonWidth, buttonHeight), "SaveAudio")){
saveAudio();
}
if(Microphone.IsRecording(deviceName)){
GUI.Label(Rect(buttonWidth +50, 10, buttonWidth, buttonHeight), "Recording");
}else{
GUI.Label(Rect(buttonWidth +50, 10, buttonWidth, buttonHeight), "Not Recording");
}
}
}
Two things i found:
if i add to getAudioSource, where microphone register audio datas , .clip i can play it but then i can't save with SavWav class. there is any other way to play what i recorded without putting .clip to getAudioSource?
What do you mean you "can't"? Do you get an error message? Can we get more info?
Yes this is the message i get:
$$anonymous$$issing$$anonymous$$ethodException: UnityEngine.AudioClip.Play
Boo.Lang.Runtime.DynamicDispatching.$$anonymous$$ethodDispatcherFactory.ProduceExtensionDispatcher ()
Boo.Lang.Runtime.DynamicDispatching.$$anonymous$$ethodDispatcherFactory.Create ()
Boo.Lang.Runtime.RuntimeServices.DoCreate$$anonymous$$ethodDispatcher (System.Object target, System.Type targetType, System.String name, System.Object[] args)
Boo.Lang.Runtime.RuntimeServices.Create$$anonymous$$ethodDispatcher (System.Object target, System.String name, System.Object[] args)
Boo.Lang.Runtime.RuntimeServices+<Invoke>c__AnonStorey14.<>m__7 ()
Boo.Lang.Runtime.DynamicDispatching.DispatcherCache.Get (Boo.Lang.Runtime.DynamicDispatching.Dispatcher$$anonymous$$ey key, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cache$$anonymous$$eyName, System.Type[] cache$$anonymous$$eyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[] args, System.String cache$$anonymous$$eyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args)
UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args, System.Type scriptBaseType)
audioRegisterTest.playAudio () (at Assets/Scripts/audioRegisterTest.js:40)
audioRegisterTest.OnGUI () (at Assets/Scripts/audioRegisterTest.js:56)
Answer by Statement · Jul 17, 2013 at 01:54 PM
AudioClip doesn't have a Play method, but AudioSource do.
private var getAudioSource : AudioClip;
...
getAudioSource.Play(); // MissingMethodException: UnityEngine.AudioClip.Play
the problem is that if i want to use SavWav class i've to keep it as AudioClip and not AudioSource.
Yeah, but look at the docs for AudioSource, you can play clips with it.
Answer by LemonLake · Jul 26, 2013 at 09:52 AM
You need to give the AudioSource an AudioClip to play, then call the Play() method on your AudioSource.