In android audio recording is simple task but when it comes to record in background it becomes little bit difficult so here we are gonna explore audio recording with service.
How to record a audio when the app is running in background ?
In Android any task which has to be done in background we have to use extend Service Class.
How does this Service Class works?A
Service
is an application component that can perform long-running operations in
the background and does not provide a user interface. Another
application component can start a service and it will continue to run in
the background even if the user switches to another application.
Additionally, a component can bind to a service to interact with it and
even perform interprocess communication (IPC). For example, a service
might handle network transactions, play music, perform file I/O, or
interact with a content provider, all from the background.AudioService.class
public class AudioService extends Service
implements MediaRecorder.OnInfoListener { private MediaRecorder mRecorder; //setting maximum file size to be recorded private long Audio_MAX_FILE_SIZE = 1000000;//1Mb private int[] amplitudes = new int[100]; private int i = 0; private File mOutputFile;
@Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags,
int startId) { super.onStartCommand(intent, flags, startId); return Service.START_STICKY; } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); startRecording(); }
@Nullable @Override public IBinder onBind(Intent intent) { return null; }
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setOnInfoListener(this);
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setMaxFileSize(Audio_MAX_FILE_SIZE);
mRecorder.setOutputFormat
(MediaRecorder.OutputFormat.MPEG_4); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{ mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC); mRecorder.setAudioEncodingBitRate(48000); } else { mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); mRecorder.setAudioEncodingBitRate(64000); } mRecorder.setAudioSamplingRate(16000); mOutputFile = getOutputFile(); mOutputFile.getParentFile().mkdirs(); mRecorder.setOutputFile(mOutputFile.getAbsolutePath()); try { mRecorder.prepare(); mRecorder.start(); mStartTime = SystemClock.elapsedRealtime(); } catch (IOException e) { } }
protected void stopRecording(boolean saveFile) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
mStartTime = 0;
if (!saveFile && mOutputFile != null) {
mOutputFile.delete();
}
// to stop the service by itself
stopSelf();
}
private File getOutputFile() { SimpleDateFormat dateFormat = new SimpleDateFormat
("yyyyMMdd_HHmmssSSS", Locale.US);
return new File(Environment.getExternalStorageDirectory()
.getAbsolutePath().toString() + "/Voice Recorder/RECORDING_" + dateFormat.format(new Date()) + ".m4a"); }
@Override public void onInfo(MediaRecorder mr, int what, int extra) { //check whether file size has reached to 1MB to stop recording if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_FILESIZE_REACHED) { stopRecording(true); } } @Override public void onDestroy() { super.onDestroy(); stopRecording(true); }
To stop or start the recording or service manually
In Fragment:
Start Service->
getActivity().startService(
new Intent(getActivity(), AudioService.class));
Stop Service->getActivity().stopService(
new Intent(getActivity(), AudioService.class));
In Activity:Start Service->
startService(new Intent(ClassName.this, AudioService.class));Stop Service->
stopService(new Intent(getApplicationContext(),
AudioService.class)); or stopService(new Intent(ClassName.this,
AudioService.class));
Where you have defined mStartTime variable?
ReplyDelete@Ambuj you can define or declare mStartTime variable globally in your service class
Deleteprivate long mStartTime;
DeleteHow I use the fragment to start?
ReplyDelete