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();
}
Related
I created an audio file with file extension .aac using MediaRecorder. Now, i want to zip this single file and save in android programmatically. Thanks in advance.
Below is my code to record audio and save file as .aac
private void startRecording(){
fileName=context.getExternalCacheDir().getAbsolutePath();
fileName +="/"+ Calendar.getInstance().getTimeInMillis()+".aac";
executorService.execute(new Runnable() {
#Override
public void run() {
mediaRecorder=new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
mediaRecorder.setOutputFile(fileName);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mediaRecorder.setMaxDuration(600000);
mediaRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
#Override
public void onInfo(MediaRecorder mr, int what, int extra) {
if(what==MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED){
((Activity)context).runOnUiThread(new Runnable() {
#Override
public void run() {
setText("Start Recording");
}
});
mStartRecording=false;
onRecord(mStartRecording);
}
}
});
try {
mediaRecorder.prepare();
} catch (IOException e) {
throw new RuntimeException(e);
}
mediaRecorder.start();
}
});
}
in my android app , i need to record audio for only 1 min? can any one help me to record audio for a certain amount of time? i ve written a code for recording audio and it will stop only when the stop button is pressed but i need to stop recording automatically after 1 min...
here my code .. but it doesnt work
try {
recorder.prepare();
recorder.start();
//Declare the timer
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(AudioRecordingActivity.this, "record successfullly ",
Toast.LENGTH_SHORT).show();
i1=i1+1;
}
});
//Called each time when 1000 milliseconds (1 second) (the period parameter)
}
},
//Set how long before to start calling the TimerTask (in milliseconds)
0,
//Set the amount of time between each execution (in milliseconds)
10*1000);
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
Toast.makeText(AudioRecordingActivity.this, "record successfullly",
Toast.LENGTH_SHORT).show();
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Try below code
recorder.start();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
recorder.stop();
}
}, 60000);
Try recorder.setMaxDuration(60*1000);
Or, if you need to do this repeatetly, use above answer, but add
if (null == recorder)
{
recorder = new MediaRecorder();
} else recorder.reset();
This approach more likely :)
Use below code.
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
new Timer().schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
});
}
}, 60000);
}
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Hello i have create a small application in which i am recording audio. My problem is, in following code i can create a file in specific folder but when i am trying to play this audio file it says this media file is not supporting. plz help
public void startRecording() {
System.out.println("STart Recording");
if (recorder != null) {
recorder.release();
} else {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile(getFilePath());
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
recorder.release();
}
}
}
private void stopRecording() {
System.out.println("Stop Recording");
try {
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
private String getFilePath() {
File rsd = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
if (!rsd.isDirectory()) {
rsd.mkdir();
}
File dcim = new File(rsd + "/hello.mp3");
return dcim.getAbsolutePath();
}
Yup like Ralph House said in comment, you should save your file with .3gp extention. Change
File dcim = new File(rsd + "/hello.mp3");
To:
File dcim = new File(rsd + "/hello.3gp");
How to implement sound waves during recording sound in android??
in the below code
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
resultView = (TextView) findViewById(R.id.output);
try {
// the soundfile
File storageDir = new File(Environment
.getExternalStorageDirectory(), "com.hascode.recorders");
storageDir.mkdir();
Log.d(APP_TAG, "Storage directory set to " + storageDir);
outfile = File.createTempFile("hascode", ".3gp", storageDir);
// init recorder
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(outfile.getAbsolutePath());
// init player
player.setDataSource(outfile.getAbsolutePath());
} catch (IOException e) {
Log.w(APP_TAG, "File not accessible ", e);
} catch (IllegalArgumentException e) {
Log.w(APP_TAG, "Illegal argument ", e);
} catch (IllegalStateException e) {
Log.w(APP_TAG, "Illegal state, call reset/restore", e);
}
btRecord = (Button) findViewById(R.id.btRecord);
btRecord.setOnClickListener(handleRecordClick);
btPlay = (Button) findViewById(R.id.btPlay);
btPlay.setOnClickListener(handlePlayClick);
}
private final OnClickListener handleRecordClick = new OnClickListener() {
#Override
public void onClick(View view) {
if (!recording) {
startRecord();
} else {
stopRecord();
}
}
};
private final OnClickListener handlePlayClick = new OnClickListener() {
#Override
public void onClick(View view) {
if (!playing) {
startPlay();
} else {
stopPlay();
}
}
};
private void startRecord() {
Log.d(APP_TAG, "start recording..");
printResult("start recording..");
try {
recorder.prepare();
recorder.start();
recording = true;
} catch (IllegalStateException e) {
Log
.w(APP_TAG,
"Invalid recorder state .. reset/release should have been called");
} catch (IOException e) {
Log.w(APP_TAG, "Could not write to sd card");
}
}
private void stopRecord() {
Log.d(APP_TAG, "stop recording..");
printResult("stop recording..");
recorder.stop();
recorder.reset();
recorder.release();
recording = false;
}
private void startPlay() {
Log.d(APP_TAG, "starting playback..");
printResult("start playing..");
try {
playing = true;
player.prepare();
player.start();
} catch (IllegalStateException e) {
Log.w(APP_TAG, "illegal state .. player should be reset");
} catch (IOException e) {
Log.w(APP_TAG, "Could not write to sd card");
}
}
private void stopPlay() {
Log.d(APP_TAG, "stopping playback..");
printResult("stop playing..");
player.stop();
player.reset();
player.release();
playing = false;
}
private void printResult(String result) {
resultView.setText(result);
}
Do you want to merge your recorded sound with a pre-recorded sound of waves?
If so, maybe you could try to create two sound objects of the different sources. Then you maybe could write to the output file in intervals. One interval for the pre-recorded and one for the fresh one.
I actually don't know if that's even a valid way to do this. Just what my general approach would've been facing this problem.
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);