Record audio of one minute - android

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);

Related

"MediaRecorder: start failed: -38" when trying to record a video [duplicate]

i searched to check if this question is no dup , i see some has no answer and others did not help.
this is my code :
private void startRecording()
{
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/recordedHeckPost_.3gp";
mRecorder.setOutputFile(mFileName);
try {
mRecorder.prepare();
//Thread.sleep(2000);
mRecorder.start();
}
catch (InterruptedException e)
{ // TODO Auto-generated catch block
e.printStackTrace();
}
catch(IllegalStateException e)
{
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void stopRecording()
{
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
After running this code (On Nexus 5) i get the below start failed -38 exception:
05-31 18:17:39.404: E/MediaRecorder(2464): start failed: -38
05-31 18:17:39.404: W/System.err(2464): java.lang.IllegalStateException
05-31 18:17:39.404: W/System.err(2464): at android.media.MediaRecorder.start(Native Method)
Thanks.
Found the solution , it appears i had some other service in the background which is using
AudioRecord and uses the mic as well.... so thats the -38 :)
In my case that error (MediaRecorder: start failed: -38) was appearing after switching to second camera when I forgot to release MediaRecorder during closing (first) camera:
mediaRecorder?.release()
mediaRecorder = null

How to record audio file using timer

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.

Cant find the stored audio file in my internal storage

Am trying to record audio and store this audio in my internal storage not sdcard. but after following tutorials i cant find my recorded audio in my device
public void record(){
new CountDownTimer(5000, 1000) { // 5000 = 5 sec
public void onTick(long millisUntilFinished) {
startRecording();
Toast.makeText(HomeActivity.this, R.string.record_audio, Toast.LENGTH_LONG).show();
}
public void onFinish() {
stopRecording();
Toast.makeText(HomeActivity.this, R.string.record_saved, Toast.LENGTH_LONG).show();
}
}.start();
}
public void startRecording() {
if(mRecorder!=null){
mRecorder.release();
}
File fileOut = new File(File);
if(fileOut!=null){
fileOut.delete();
}
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(File);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mRecorder.start();
}

In Android, how do I sync audio recording with simultaneous playback?

I'm trying to write a function that will record the users voice while an instrumental is playing in the background.
I have been fairly successful in getting 2 MediaPlayers to start at almost the exact same time. Using Threads and the CyclicBarrier, I have been able to start 2 MediaPlayer within 5 milliseconds of each, which sounds pretty good.
The problem is that whenever I attempt to start a MediaRecorder and a MediaPlayer at the same time, there is a noticeable delay between them. This is problematic, because when I playback the recorded audio and the instrumental, they are not in sync with each other because the recorded started at a different time than the player.
Here is the function and the 2 Threads that I'm trying to use for the Recorder and Player. I apologize for the length, but if anybody has any better ideas I would be very grateful:
private void onRecord(boolean start) {
if (start) {
final CyclicBarrier recordBarrier = new CyclicBarrier(2);
final CyclicBarrier postrecordBarrier = new CyclicBarrier(2);
Thread beatTask = new Thread(new RecordBeatPlayTask(recordBarrier, postrecordBarrier), "Thread 1");
Thread recordTask = new Thread(new RecordVoiceRecordTask(recordBarrier, postrecordBarrier), "Thread 2");
beatTask.start();
recordTask.start();
}
else {
mRecorder.stop();
mRecordBeatPlayer.stop();
mRecorder.release();
mRecorder = null;
mRecordBeatPlayer.release();
mRecordBeatPlayer = null;
}
}
private static class RecordVoiceRecordTask implements Runnable {
private CyclicBarrier barrier;
private CyclicBarrier barrier2;
public RecordVoiceRecordTask(CyclicBarrier barrier, CyclicBarrier barrier2) {
this.barrier = barrier;
this.barrier2 = barrier2;
}
public void run() {
try {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorderPath = Environment.getExternalStorageDirectory().getAbsolutePath();
mRecorderPath += "/HypeHopRecording.mp4";
mRecorder.setOutputFile(mRecorderPath);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e("Recorder failed", "prepare() failed");
}
barrier.await();
mRecorder.start();
barrier2.await();
System.out.println(Thread.currentThread().getName() + " has crossed the barrier");
} catch (InterruptedException ex) {
Logger.getLogger(RecordActivity.class.getName()).log(Level.SEVERE, null, ex);
} catch (BrokenBarrierException ex) {
Logger.getLogger(RecordActivity.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private static class RecordBeatPlayTask implements Runnable {
private CyclicBarrier barrier;
private CyclicBarrier barrier2;
public RecordBeatPlayTask(CyclicBarrier barrier, CyclicBarrier barrier2) {
this.barrier = barrier;
this.barrier2 = barrier2;
}
public void run() {
try {
mRecordBeatPlayer = new MediaPlayer();
try {
mRecordBeatPlayer.setDataSource(mBeatPath);
mRecordBeatPlayer.prepare();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
barrier.await();
mRecordBeatPlayer.start();
barrier2.await();
System.out.println(Thread.currentThread().getName() + " has crossed the barrier");
} catch (InterruptedException ex) {
Logger.getLogger(RecordActivity.class.getName()).log(Level.SEVERE, null, ex);
} catch (BrokenBarrierException ex) {
Logger.getLogger(RecordActivity.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
basically, there is deplay between 'mRecordBeatPlayer.start()' and user can hear the sound, you have to test out this deplay.

How to change frequency of audio file and save it as mp4 file on sdcard?

I m trying to make an app where user can record his voice and can play the voice in funny tone. Like talking tom application but not just that..He shd be able to share that recorded sound file.
I m able to record the sound but not able to save the file in funny tone..its getting saved in original tone.
any suggestions?
private void startRecording() {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.mp4";
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
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;
}

Categories

Resources