Service class to record audio in background :
below is the service to record audio from background and save them to sdcard on desire location.
public class AudioRecorderService extends Service
{
private MediaRecorder myRecorder;
// private String outputFile = null;
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public void onCreate()
{
// Start foreground service to avoid unexpected kill
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
{
Notification notification = null;
notification = new Notification.Builder(this)
.setContentTitle("Recording Audio")
.setContentText("")
.setSmallIcon(R.mipmap.ic_launcher)
.build();
startForeground(22, notification);
}
File imagesFolder = new File(
Environment.getExternalStorageDirectory(), "folder name");
if (!imagesFolder.exists())
imagesFolder.mkdirs(); // <----
File audio = new File(imagesFolder, System.currentTimeMillis()
+ ".3gpp");
myRecorder = new MediaRecorder();
myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myRecorder.setOutputFile(audio.getPath());
try
{
myRecorder.prepare();
myRecorder.start();
}
catch (Exception | Error e)
{
// start:it is called before prepare()
// prepare: it is called after start() or before setOutputFormat()
e.printStackTrace();
}
}
public int onStartCommand(Intent intent, int flags, int startId)
{
//onCreate();
// // return 1;
// return START_STICKY; //not sure about this one
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy()
{
try
{
myRecorder.stop();
myRecorder.reset();
myRecorder.release();
myRecorder = null;
}
catch (Exception | Error e)
{
e.printStackTrace();
}
}
}
just fire a intent for this service ,it will start recording audio & stop service to stop recording.
to start recording :
startService(new Intent(context, AudioRecorderService.class));
to stop recording :
stopService(new Intent(context, AudioRecorderService.class));
0 comments:
Post a Comment