Import java. Io. file;
Import java. Io. ioexception;
Import Android. Media. mediarecorder;
Import Android. OS. environment;
Public class audiorecorder
{
Final mediarecorder recorder = new mediarecorder ();
Final string path;
Public audiorecorder (string path)
{
This. Path = sanitizepath (PATH );
}
Private string sanitizepath (string path)
{
If (! Path. startswith ("/") {
Path = "/" + path;
}
If (! Path. Contains (".") {
Path + = ". 3GP ";
}
Return environment. getexternalstoragedirectory (). getabsolutepath () + path;
}
/**
* Start record
*/
Public void start () throws ioexception
{
String state = Android. OS. environment. getexternalstoragestate ();
If (! State. Equals (Android. OS. environment. media_mounted )){
Throw new ioexception ("SD card is not mounted. It is" + state + ".");
}
File directory = new file (PATH). getparentfile ();
If (! Directory. exists ()&&! Directory. mkdirs ()){
Throw new ioexception ("path to file cocould not be created .");
}
Recorder. setaudiosource (mediarecorder. audiosource. Mic );
Recorder. setoutputformat (mediarecorder. outputformat. three_gpp );
Recorder. setaudioencoder (mediarecorder. audioencoder. amr_nb );
Recorder. setoutputfile (PATH );
Recorder. Prepare ();
Recorder. Start ();
}
Public void stop () throws ioexception {
Recorder. Stop ();
Recorder. Release ();
}
}