How to record audio file using timer - android

Hi i am new for android and in my app i have to record audio using timer as like my below image, Using my below i can able record audio but how can do this scenario with help of timer please help me some
My code:-
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.record_button:
startRecording()
break;
case R.id.stop_button:
break;
}
}
private void startRecording() {
try {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.prepare();
mRecorder.start();
} catch (Throwable t) {
t.printStackTrace();
}
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}

Just start a timer in startRecording and stop it in stopRecording. See: https://stackoverflow.com/a/3734070/2324204

You can use a Chronometer which already exists in Android.
Note: Chronometer is a widget that extends TextView so replace your current TextView with the Chronometer.
Example with your code would be:
//....
Chronometer simpleChronometer = (Chronometer) findViewById(R.id.simpleChronometer);
//...
private void startRecording() {
try {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.prepare();
mRecorder.start();
simpleChronometer.start(); // start a chronometer
//simpleChronometer.setFormat("Time Running - %s"); // set the format for a chronometer
} catch (Throwable t) {
t.printStackTrace();
}
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
simpleChronometer.stop();
}
In case you want to do things manually you can use the Stopwatch class.

Related

Audio recorder restarting

I've create Audio recorder. When rotating my device the audio will be automatically saved. How can I avoid this ? I'm using Android 4.1.1 on device.
private MediaRecorder mRecorder = null;
private static final String LOG_TAG = "MediaAudioCapture";
ImageButton button_audio_button = (ImageButton) findViewById(R.id.audio_button);
button_audio_button.setOnClickListener(new OnClickListener() {
public void onClick(View record_button) {
ImageButton audio_button = (ImageButton) record_button;
if (!started_note_recording) {
if (!started_recording) {
startRecording();
audio_button.setImageResource(R.drawable.audio_red);
} else {
stopRecording();
audio_button.setImageResource(R.drawable.audio);
}
}
}
});
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setOutputFile(output.getAbsolutePath());
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
ImageButton button_audio_button = (ImageButton)findViewById(R.id.audio_button);
button_audio_button.setImageResource(R.drawable.audio);
}
}
});
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
stopWatch.start();
}
That's because when you rotate, activity will be reload. You can fix the orientation to portrait or you have to change your logic to retain the state on rotation.

Lag on my buttons

I made a code which allow me to record the audio from the microphone. Strange thing is that when I click my button it appears to have some kind of lag, the button remains visually pressed for half a second and then starts it method. is there a reason for this, and how could I solve this?
What I am having right now:
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.bRecord:
if (mStartRecording) {
mRecordButton.setText("Stop Recording");
onRecord(true);
mStartRecording = false;
} else {
mRecordButton.setText("Start Recording");
mStartRecording = true;
onRecord(false);
}
break;
}
EDIT
Thanks for the help, I decided to work with a service but am struggling a little bit. I created:
public class RecordService extends IntentService {
private MediaRecorder mRecorder = null;
private String mFileName;
private static final String LOG_TAG = "RecordService";
public RecordService() {
super("RecordService");
// TODO Auto-generated constructor stub
}
#Override
protected void onHandleIntent(Intent intent) {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
}
and
case R.id.bRecord:
if (mStartRecording) {
mRecordButton.setText("Stop Recording");
//onRecord(true);
Intent service = new Intent(myContext, RecordService.class);
myContext.startService(service);
mStartRecording = false;
} else {
mRecordButton.setText("Start Recording");
mStartRecording = true;
onRecord(false);
}
break;
Having a question about this, how can I now stop this service (= stop recording) (by clicking f.e. the button again) and communicate an idea back to the activity? I tried to add a stop function in the service but it is not seem to work when I call service.stop() to stop it..
My guess is that since you're starting the recording in the same thread as the UI, the UI doesn't respond until all the code in startRecording() has finished executing. You might want to consider calling onRecord in its own thread.
You could use AsyncTask for short Operations (a few seconds at the
most.)
Use Service for long running Operations
Attention: AsyncTask takes care of the thread handling for you. If you write a Service, you have to spawn your own thread otherwise it will run on the UI thread.
One of several ways to do it:
Service:
#Override
protected void onHandleIntent(Intent intent) {
//your recorder code
//get the messenger from intent
Bundle extras = intent.getExtras();
if (extras != null) {
Messenger messenger = (Messenger) extras.get("MESSENGER");
try {
//send a message back to the activity
messenger.send(...);
} catch (android.os.RemoteException e1) {
Log.w(getClass().getName(), "Exception sending message", e1);
}
}
}
Activity:
Definition of the handler
private Handler handler = new Handler() {
public void handleMessage(Message message) {
//do something with the message from the service
};
};
The code to start the service
Intent intent = new Intent(this, YourService.class);
Messenger messenger = new Messenger(handler);
intent.putExtra("MESSENGER", messenger);
startService(intent);

Android mediarecorder stop failed

I've faced a very strange behavior: sometimes my mediarecorder crashes with an error "Stop failed" and sometimes it works fine. Is there my fault or it is a bug of the system?
I cant't get what is wrong.
private void stopRecording(){
ticker.cancel();
ticker.purge();
recorder.stop();
startBtn.setText("Start");
recordInProcess = false;
markList = locWriteTask.getMarkArray();
mCamera.lock();
recorder.release();
}
private void startRecording(){
startBtn.setText("Stop");
recordInProcess = true;
recorder = new MediaRecorder();
mCamera.unlock();
recorder.setCamera(mCamera);
recorder.setPreviewDisplay(mSurfaceHolder.getSurface());
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
recorder.setMaxDuration((int) 10000000);
recorder.setVideoSize(320, 240);
recorder.setVideoFrameRate(15);
recorder.setOutputFile(FULL_PATH_TO_LOCAL_FILE + counter + MP4);
try{
recorder.prepare();
} catch (Exception e){
finish();
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
ticker = new Timer();
locWriteTask = new WriteTimeLocationTimerTask(ll);
ticker.schedule(locWriteTask, 0, DELAY);
recorder.start();
}
You may catch a RuntimeException at the MediaRecorder.stop() method.
Example:
MediaRecorder mRecorder = new MediaRecorder();
File mFile = new File("The output file's absolutePath");
... //config the mRecorder
mRecorder.setOutputFile(mFile.getAbsolutePath());
... //prepare() ...
mRecorder.start();
try {
mRecorder.stop();
} catch(RuntimeException e) {
mFile.delete(); //you must delete the outputfile when the recorder stop failed.
} finally {
mRecorder.release();
mRecorder = null;
}
If the recorder is not in a recording state, then the stop could fail.
See
http://developer.android.com/reference/android/media/MediaRecorder.html
add following in your SurfaceCreated(SurfaceHolder holder):
CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH); //get your own profile
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(camcorderProfile.videoFrameWidth,camcorderProfile.videoFrameHeight);
mCamera.setParameters(parameters);
Experienced the same error: Sometimes my MediaRecorder crashed with an error "Stop failed" and sometimes it worked fine. Adding this solved my problem:
#Override
public void onStop() {
super.onStop();
if (mRecorder != null) {
mRecorder.release();
mRecorder = null;
}
}

Record audio of one minute

I am working with audio recording in Android.
I want to record audio maximum of 1 minute and if user ask to stop before one minute it should be stop.
I have record audio code and it works perfectly.
How can I set time duration it?
If solution is thread.sleep then it's ok. I do something like same:
if (start) {
startRecording();
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
//What should do ? to stop thread this Thread.sleep(60000);
callToStopRecording();
}
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(path + mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e("Camera Error", "prepare() failed");
}
mRecorder.start();
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
but at stop recoding button, what do I need to write?
You can use this solution:
mRecorder.setMaxDuration(max_duration_ms);
The duation is in ms, so you have to use:
mRecorder.setMaxDuration(6000);

Android--Mic input levels?

I want to detect blowing into the mic on android. Google wasn't much help. Is it possible?
OK! I found this online, so it definitely seems possible. It looks like you can call mediaRecorder.getMaxAmplitude().
Code from an app called NoiseAlert
public void start() {
if (mRecorder == null) {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile("/dev/null");
mRecorder.prepare();
mRecorder.start();
mEMA = 0.0;
}
}
public double getAmplitude() {
if (mRecorder != null)
return (mRecorder.getMaxAmplitude()/2700.0);
else
return 0;
}
Here's the source

Categories

Resources