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.”