- Home /
use own sound recorder android library in unity
I want record and save audio in unity3d with my own library (without unity classes); so I write a library and export jar and then import it to unity
library :
import java.io.File; import java.io.IOException;
import android.media.MediaMuxer.OutputFormat;
import android.media.MediaRecorder;
import android.media.MediaRecorder.AudioEncoder;
import android.media.MediaRecorder.AudioSource;
import android.os.Environment;
import android.util.Log;
public class SoundRecorder {
MediaRecorder mediaRecorder;
String soundName;
String diractory;
File soundFile;
File directoryFile;
String lastError = "";
boolean isInRecord = false;
void init(String directory, String soundName)
{
this.soundName = soundName;
this.diractory = directory;
directoryFile = new File(directory);
if(!directoryFile.exists())
{
directoryFile.mkdirs();
}
soundFile = new File(directoryFile, soundName);
Log.i("Unity", "@#$%^&#$@$@#$@");
}
void prepareSound()
{
try
{
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setAudioEncoder(AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(soundFile.getAbsolutePath());
mediaRecorder.prepare();
}
catch (Exception e)
{
e.printStackTrace();
lastError = e.getMessage();
}
}
public void startStopRecording()
{
if (isInRecord)
{
if(mediaRecorder != null)
{
mediaRecorder.reset();
}
} else
{
prepareSound();
mediaRecorder.start();
}
isInRecord = !isInRecord;
}
public String getLastError()
{
return lastError;
}
boolean getIsInRecord()
{
return isInRecord;
}
public String getSoundName()
{
return soundName;
}
public String getSoundDirectory()
{
return diractory;
}
public String getExternalStoragePath()
{
return Environment.getExternalStorageDirectory().getAbsolutePath();
}
}
and the unity script :
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.IO; using System;
public class SoundRecorder : MonoBehaviour
{
AndroidJavaObject soundRecorder;
void Start()
{
Application.RequestUserAuthorization(UserAuthorization.Microphone);
soundRecorder = new AndroidJavaObject("com.tafavotco.soundrecorder.SoundRecorder");
object[] args =
{
soundRecorder.Call<String>("getExternalStoragePath")+"/aaa",
"abc.amr"
};
soundRecorder.Call("init", args);
Debug.Log("Error : " + soundRecorder.Call<String>("getLastError"));
}
// Update is called once per frame
void Update()
{
}
void OnGUI()
{
if(GUI.Button(new Rect(0, 0, 200, 90), soundRecorder.Call<bool>("getIsInRecord") ? "stop" : "start"))
{
soundRecorder.Call("startStopRecording");
Debug.Log("Error : " + soundRecorder.Call<String>("getLastError"));
}
}
}
but it doesn't work and log in the log cat :
08-17 10:01:58.134: I/Unity(32673): @#$%^&#$@$@#$@
08-17 10:01:58.134: I/Unity(32673): Error :
08-17 10:01:58.134: I/Unity(32673):
08-17 10:01:58.134: I/Unity(32673): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
08-17 10:01:59.614: I/Unity(32673): AndroidJavaException: java.lang.IllegalStateException
08-17 10:01:59.614: I/Unity(32673): at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <filename unknown>:0
08-17 10:01:59.614: I/Unity(32673): at UnityEngine.AndroidJNISafe.CallVoidMethod (IntPtr obj, IntPtr methodID, UnityEngine.jvalue[] args) [0x00000] in <filename unknown>:0
08-17 10:01:59.614: I/Unity(32673): at UnityEngine.AndroidJavaObject._Call (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0
08-17 10:01:59.614: I/Unity(32673): at UnityEngine.AndroidJavaObject.Call (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0
08-17 10:01:59.614: I/Unity(32673): at SoundRecorder.OnGUI () [0x00000] in <filename unknown>:0
08-17 10:01:59.614: I/Unity(32673):
08-17 10:01:59.614: I/Unity(32673): (Filename: Line: -1)
what's the problem ?
UPDATE :
the "lastError" after click on gui button is : "setAudioSource failed."
Answer by TheDesignZoo · Dec 11, 2015 at 01:55 PM
Im sure you got this sorted. You can get this error if you don't have the correct permission in the Android manfest.xml file for audio recording when you set the audiosource on the MediaRecorder.
Maybe it should be this be updated in the manifest versus the request on the Unity side? I believe this request might be for WebPlayer?
Your answer
Follow this Question
Related Questions
No OpenGL Context in Unity above 2017.1.2f1 0 Answers
Using AndroidJavaClass in a java Unity3d plugin 0 Answers
Using Java Plugin to add an Android Edittext view 0 Answers
How to talk to Java plugin 0 Answers
How to use Android Api in Unity 1 Answer