How add effect to Media Recorder in android? - android

I Search everywhere i dont find anything.
how add effect on file audio in android?
This is my code...
mRecorder = new MediaRecorder();
mRecorder.setMaxDuration(180000);
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
mRecorder.setAudioEncodingBitRate(50000);
} else {
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mRecorder.setAudioEncodingBitRate(50000);
}
mRecorder.setAudioSamplingRate(50000);
mOutputFile = getOutputFile();
mOutputFile.getParentFile().mkdirs();
mRecorder.setOutputFile(mOutputFile.getAbsolutePath());
mRecorder.setAudioEncodingBitRate(50000);
mRecorder.setAudioSamplingRate(50000);
try {
mRecorder.prepare();
mRecorder.start();
mStartTime = SystemClock.elapsedRealtime();
mHandler.postDelayed(mTickExecutor, 100);
} catch (IOException e) {
}
Thank you very much if you help me!!!!!!

Solution : library called Sonic. Its basically for Speech as it use PSOLA algo to change pitch and tempo. But its ok.
by this library you can speeding up or slowing down speech and change the voice to child or woman or granny.

Related

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.

android - Which MediaRecorder configuration is supported by all device?

I am recording communication voice in my app and I added storage and audio record permission manifest and also getting programmatically.
My code is working fine on one device(Android 6.0 Lenovo K3 Note)
But not on another (Android 8.1 ONEPLUS A5010)
In second device output is saved as a blank file of 3.15KB
I am adding my code which I am using please tell what I am doing wrong.
MediaRecorder mRecorder;
String mFileName;
Code in OnCreate
File file = new File(getFilesDir(), "engwingoLastCall.3gp");
mFileName = file.getAbsolutePath();
try {
if(mRecorder == null) {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
}
} catch (Exception e) {
Log.d(TAG,"Recorder Error:"+e.getMessage());
}
Methods
public void startRecording() {
try {
if(mRecorder != null) {
mRecorder.prepare();
mRecorder.start();
}
} catch (Exception e) {
Log.d("Recorder", "prepare() failed");
}
}
public void stopRecording() {
if(mRecorder != null) {
try {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
} catch (IllegalStateException e) {
e.printStackTrace();
}catch (Exception e){
Log.d(TAG,e.getMessage());
}
}
}
Since you are not setting a profile with setProfile() method you may need to set audio channels, bitrate and sampling rate for audio too. Here is an example:
mRecorder.setAudioChannels(1);
// you would not want to record stereo, it is not logical
mRecorder.setAudioEncodingBitRate(128000);
// you can set it to 64000 or 96000 to lower quality, therefore decreasing the size of the audio
mRecorder.setAudioSamplingRate(44100);
// AFAIK, default value.
Hope this helps.
My Code was OK but the reason for this behavior of my recorder was,
Some other service also using my recorder at that time and that's why the file was saved empty (3.15KB Size)

Android: recording audio using MediaRecorder - file doesnt play

i´ve implemented the sample Code direct from Google using MediaRecorder
Also I have added some properties and set the filetype to ".mp3" (I know that it doesnt create a real mp3, but ".3gp" doesnt make sence to me either, I´m recording Audio not Video..)
Now my Problem is that, if I want to Play the file, it does nothing (0:00 sec). I checked the properties on the file, it fills up space on the storage.
ADDITIONAL:
Anyway I want to implement a visual Feedback while recording. Horizon seems to be the only good looking lib. But using Horizon you have to feed it with the buffer from Audiorecorder, which is not possible with MediaRecoder. Does anybody know a good looking lib that visualizes currently recording audio with MediaRecorder?
Thank you very much
private void startRecording() {
if(this.mRecorder != null)
return;
String fileToWrite = this.ProjectDirPath + File.separator + GetToday() + this.fileTpye;
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mRecorder.setOutputFile(fileToWrite);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
mRecorder.setAudioSamplingRate(16000);
mRecorder.setAudioEncodingBitRate(44100);
mRecorder.setAudioChannels(1);
try {
mRecorder.prepare();
}catch (IOException e) {
Log.e("startRecording()", "prepare() failed");
}
initRecorder();
mRecorder.start();
isRecording = true;
}
private void stopRecording() {
if(this.mRecorder == null)
return;
else{
try{
mRecorder.stop();
mRecorder.release();
mRecorder = null;
isRecording = false;
this.updateListView();
} catch(Exception e){
mRecorder = null;
isRecording = false;
}
}
}
public static String GetToday(){
Date presentTime_Date = Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(presentTime_Date);
}
Permissions are set in the manifest and at runtime.
Finally found myself one good lib.
https://github.com/adrielcafe/AndroidAudioRecorder?utm_source=android-arsenal.com&utm_medium=referral&utm_campaign=4099

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 implementing setOnInfoListener -max duration

I 'm using mediarecorder to capture audio through MIC. I have set the max duration to 20 seconds. The recording stops automatically and does not stop at my break point inside setOnInfoListener.
**UPDATE: Changed my code according to suggestion but still doesnt stop at the breakpoint inside the listener.**
mRecorder.reset();
mRecorder.setOnInfoListener(new OnInfoListener() {
#Override
public void onInfo(MediaRecorder mr, int what, int extra) {
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
mRecorder.stop();
}
}
});
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setAudioSamplingRate(8000);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.RAW_AMR);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile(fileName);
mRecorder.setMaxDuration(20000);
try {
mRecorder.prepare();
} catch(IOException exception) {
mRecorder.reset();
mRecorder.release();
mRecorder = null;
return;
}
mRecorder.start();
Can someone please tell me why does the code not hit my onInfo method inside the listener rather silently stops recording.
Thanks
When you set the output format, try using THREE_GPP instead of RAW_AMR.
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
According to the documentation for setOutputFormat():
It is recommended to always use 3GP format when using the
H.263 video encoder and AMR audio encoder. Using an MPEG-4
container format may confuse some desktop players.
Try moving your call to setOnInfoListener() before the call to prepare().
In my own video capture code, I invoke setOnInfoListener() right after creating the MediaRecorder object. In your code example, a good place might be right after reset() and before setAudioSource().
Otherwise, the body of your OnInfoListener class looks correct.
I've added the MediaRecorder setup code from my app, which does work correctly.
try {
mCamera.unlock();
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setOnErrorListener( new VideoRecorderErrorListener() );
mMediaRecorder.setOnInfoListener( new VideoRecorderInfoListener() );
// As per Android API docs, the ordering of the following initialization
// calls is important.
mMediaRecorder.setCamera(mCamera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setOutputFile( mOutputFilePath );
mMediaRecorder.setMaxFileSize(VIDEO_MAX_FILE_SIZE);
mMediaRecorder.setAudioChannels(AUDIO_CHANNELS);
mMediaRecorder.setAudioSamplingRate(AUDIO_SAMPLING_RATE);
mMediaRecorder.setAudioEncodingBitRate(AUDIO_ENCODING_BIT_RATE);
mMediaRecorder.setMaxDuration(VIDEO_MAX_DURATION);
mMediaRecorder.setVideoFrameRate(mPictureFPS);
mMediaRecorder.setVideoEncodingBitRate(VIDEO_ENCODING_BIT_RATE);
mMediaRecorder.setVideoSize(mPreviewWidth, mPreviewHeight);
mMediaRecorder.setPreviewDisplay(mHolder.getSurface());
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (IllegalStateException e) {
as soon as it will reach the maximum duration, it will call the (Customized Method) stopRecording(): Where we can handle all the stop recording and release player camera and preview.
myRecorder = new MediaRecorder();
myRecorder.setOnInfoListener(new MediaRecorder.OnInfoListener() {
#Override
public void onInfo(MediaRecorder mr, int what, int extra) {
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
stopRecording();
}
}
});
u passed the MediaRecorder mr object into your method but you didn't use it. Try mr.stop();

Categories

Resources