Increasing quality of recorded sound in android [duplicate] - android

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Improve Audio Recording quality in android?
I want to record audio to a 3gp file. I can do it without any problem and can hear the voice. But the voice i can hear is very slow and not clear. To increase voice quality i wrote a program but get java.lang.RuntimeException: start failed
public void onClick(View arg0)
{
root=Environment.getExternalStorageDirectory();
audiofile=new File(root,"sound.3gp");
if(!audiofile.exists())
{
Log.w(TAG, "File doesn't exists");
try
{
audiofile.createNewFile();
} catch (IOException e)
{
Log.w(TAG, "Unable to create audio file",e);
}
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile(audiofile.getAbsolutePath());
try
{
recorder.setAudioSamplingRate(10);
recorder.setAudioEncodingBitRate(20);
recorder.prepare();
} catch (IllegalStateException e)
{
Log.w(TAG, "This is IllegalStateException");
} catch (IOException e)
{
Log.w(TAG, "This is IoException");
}
recorder.start();
}
what is wrong in my code? thanks.

As per my Existing Answer on Stackoverflow I provided you the below code to improve your sound quality.
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setAudioEncoder(MediaRecorder.getAudioSourceMax());
recorder.setAudioEncodingBitRate(16);
recorder.setAudioSamplingRate(44100);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();

i wrote a program but get java.lang.RuntimeException: start failed
Did u make sure at first run of your application you called in onStop
recorder.stop();
recorder.reset();
recorder.release();
I am pretty sure recorder is already in used when you are trying to run this program.thats why you are getting this error.always make sure you call above methods in onStop.Same goes true for Camera also.

Related

Is there a service which provides audio recording within an app?

I'm developing an app that would be utilizing the phone's microphone to record and store audio. However, the quality is horrible.
There are several voice recording apps that use the same mic, but their quality is exceptional.
Are there any services which would allow me to achieve this? I can recall Twilio offering something like this before, but it seems to have stopped. Basically users would be able to record audio clips and then store them for playback later. If a service can do either or both, it would be perfect.
Are you aware of any such service?
You can start MediaRecorder in this way. Here key is setAudioEncodingBitRate and setAudioSamplingRate
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioEncodingBitRate(384000);
recorder.setAudioSamplingRate(48000);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setOutputFile(outputFile); // path of your recording in phone storage
try {
recorder.prepare();
recorder.start();
} catch (IOException e) {
Log.d(TAG, "onCreate: " + e);
}
And you can stop Media recording in this way:
private void stopRecording() {
File file = new File(outputFile); // this is result of recording, you can play this file via MediaPlayer
try {
if (recorder != null) {
recorder.stop();
recorder.release();
recorder = null;
}
} catch (Exception e) {
Log.d(TAG, "stopMediaRecording: ");
} }

Unable to record audio of Incoming and Outgoing phone call in Android

Unable to record audio of Incoming and Outgoing phone call in Android
I am using Broadcastreceiver for detecting Phonecalls, It is working fine.
When ever phonecall is started I am using below code for start recording Phonecall and creating a folder of "CALLLOG", in which each call record will be stored.
public void startRecordingStoreFile(){
String out = new SimpleDateFormat("dd-MM-yyyy_hh-mm-ss").format(new Date());
File sampleDir = new File(Environment.getExternalStorageDirectory(), "/CALLLOG");
if (!sampleDir.exists()) {
sampleDir.mkdirs();
}
String file_name = "Rec_"+out;
try {
audiofile = File.createTempFile(file_name, ".amr", sampleDir);
} catch (IOException e) {
e.printStackTrace();
}
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audiofile.getAbsolutePath());
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
recordstarted = true;
}
Below code for stopping the record
public void stopRecording(){
if (recordstarted) {
recorder.stop();
audioManager.setMode(AudioManager.MODE_NORMAL);
recordstarted = false;
}
}
The extension of audio files are ".amr".
Above code is not recording the audio of a phonecall, it is creating a folder of "CALLLOG" and ".amr" files are stored but audio is not recording.I was working on this from 2 days.
For example suppose lets say I am calling to "X" person,
1.MIC is not recording once the "X"(other) person lift the call, until then audio is recording some times,
2.Some times MIC instance is not available as mentioned below solution by Afsar,
I have tried with below code but it doesn't work(Sometimes it works, sometimes not).
I am unable to record audio of Incoming and outgoing calls.Some times it works, sometimes it is not working.
Please help me on this.
Thanks in Advance.
I had the same issue in the past I was trying to record Audio + Video during video call. While device is in call MIC is being used by other processes, so before setting MediaRecorder AudioSource as MIC just check whether MIC instance is available or not. You can test it like that
private boolean validateMicAvailability(){
Boolean available = true;
AudioRecord recorder =
new AudioRecord(MediaRecorder.AudioSource.MIC, 44100,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_DEFAULT, 44100);
try{
if(recorder.getRecordingState() != AudioRecord.RECORDSTATE_STOPPED ){
available = false;
}
recorder.startRecording();
if(recorder.getRecordingState() != AudioRecord.RECORDSTATE_RECORDING){
recorder.stop();
available = false;
}
recorder.stop();
} finally{
recorder.release();
recorder = null;
}
return available;
}
Simple solution of this problem is to use some CallRecorder Library following is the link.
aykuttasil/CallRecorder check it.

MediaRecorder not working

i am using this code to record audio file in android device
private int output_formats[] = {MediaRecorder.OutputFormat.MPEG_4};
private String file_exts[] = {AUDIO_RECORDER_FILE_EXT_MP3};
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(output_formats[currentFormat]);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setAudioEncodingBitRate(16);
recorder.setAudioSamplingRate(44100);
recorder.setOutputFile(getFilename());
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
i am using 4.4.2 android device.
now my app is in both android as well as ios device so my recorded audio from 4.4.2 android device not working in ios device
how can i resolved this?
but this code working properly from other devices i checked using lollipop 5.0 or 6.0 android devices. all recorded audio working properly in ios device.
error in ios:
Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo={NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x7ca807b0 {Error Domain=NSOSStatusErrorDomain Code=-16170 "(null)"}, NSLocalizedFailureReason=An unknown error occurred (-16170)}

android: record audio with MediaPlayer on emulator

I am trying to record audio from the microphone on the Android emulator with this code:
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(Environment.getExternalStorageDirectory() + "/test/test.3gp");
try {
recorder.prepare();
}
catch (IOException io) {
Log.v(LOG_TAG, "Could not prepare the audio " + io.getMessage());
}
recorder.start();
For stopping the audio, this is the code:
recorder.stop();
recorder.reset();
recorder.release();
The recording process works fine but the resulting audio that is distorted. When I record an audio for 60 seconds duration and play it, it's duration is being shown as 120 seconds. The measurement is not exact but the this is just to give you an idea.
Only the AMR_NB encoder is working on my emulator. I have tried different output formats but the result is always the same.
Is it a limitation of the emulator or am I doing something wrong here?
Edit 1:
I have tried the AudioRecord class too and the result is the same dragging audio.
Thanks.
I have been working for the same and found the solution, Try using the following code:
private void startRecording()
{
this.recorder = new MediaRecorder();
this.recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
this.recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
this.recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
MediaRecorder.getAudioSourceMax();
this.recorder.setOutputFile(this.getFilename());
this.recorder.setOnErrorListener(this.errorListener);
this.recorder.setOnInfoListener(this.infoListener);
try
{
this.recorder.prepare();
this.recorder.start();
} catch (final IllegalStateException e)
{
e.printStackTrace();
} catch (final IOException e)
{
e.printStackTrace();
}
}
This is working perfactly. Hope it helps you :)

MediaRecorder video framerate

I'm recording videos with MediaRecorder, but it appears that whatever setting I use, the framerate is appalling (~ 1fps)
This is my code:
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile cp = CamcorderProfile.get(HIGH_QUALITY ? CamcorderProfile.QUALITY_HIGH : CamcorderProfile.QUALITY_LOW);
System.out.println("RECORDING AT " + cp.videoFrameRate); // Says 30fps
recorder.setProfile(cp);
recordingFilename = tempFileName();
recorder.setOutputFile(recordingFilename);
recorder.setPreviewDisplay(holder.getSurface());
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
finish();
} catch (IOException e) {
e.printStackTrace();
finish();
}
recorder.start();
It appears that it is the ROM I am using. I didn't realise I get the same crappy frame rate using the standard Camera app when recording video.
Nevermind :)

Categories

Resources