Android: recording audio using MediaRecorder - file doesnt play - android

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

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)

How add effect to Media Recorder in 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.

MediaRecorder stop failed: -1007

I am using a SurfaceView which shows the stream of the drone on my smartphone and what I want is to record the stream of the drone, but it's not that easy. So I think I could use MediaRecorder for this. For this problem I have read many posts, but they didn't help me out.
In the OnCreate I am creating the SurfaceView:
mPreview = FindViewById<SurfaceView>(Resource.Id.surfaceView1);
holder = mPreview.Holder;
holder.AddCallback(this);
holder.SetFormat(Format.Rgba8888);
In the SurfaceCreated override I added the SetPreviewDisplay:
mRecorder = new Android.Media.MediaRecorder();
mRecorder.SetPreviewDisplay(mPreview.Holder.Surface);
So, when I click on the record button I start the following method in a new thread, otherwise the SurfaceView will be stuck:
private void StartRecord()
{
var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
string path = System.IO.Path.Combine(sdCardPath, "test.mp4");
file = new File(path);
mRecorder.SetVideoSource(Android.Media.VideoSource.Surface);
mRecorder.SetOutputFormat(Android.Media.OutputFormat.Mpeg4);
mRecorder.SetVideoEncoder(Android.Media.VideoEncoder.H264);
mRecorder.SetVideoEncodingBitRate(512 * 1000);
mRecorder.SetVideoFrameRate(30);
mRecorder.SetOutputFile(file.AbsolutePath);
mRecorder.SetVideoSize(mPreview.Width, mPreview.Height);
mRecorder.Prepare();
mRecorder.Start();
}
There are no errors until the mRecorder.Stop() will be called:
private void StopRecord()
{
if(mRecorder != null)
{
try
{
mRecorder.Stop();
}
catch (Java.Lang.RuntimeException e)
{
// Here he always comes
file.Delete();
System.Console.WriteLine(e);
}
finally
{
mRecorder.Release();
mRecorder = null;
}
}
}
When I check into the mRecorder variable, it's saying at the surface object: failed to get surface. That's strange because when I check the mRecorder variable after the mRecorder started, it just has a surface object.
What am I doing wrong?

IllegalStateException when MediaRecorder is recording: how to fix

I got this issue and cant get my head around it, im recording calls directly from the speaker, so when i get TelephonyManager.CALL_STATE_OFFHOOK I instantly start recording audio from VOICE_CALL. thats part its ok, start recording but if the call is ended and start a new one, I get a java.lang.IllegalStateException
I think this is because the first call it's still being recorded... I've tried to do:
mRecorder.stop();
mRecorder.release();
mRecorder.reset();
but no luck, they all gave me a illegalStateException, I just want to know how to stop first call recording and record a new one without errors.
Here's my code for recording and call handling,
//At least one call exists that is dialing, active, or on hold, and no calls are ringing or waiting.
if (state == TelephonyManager.CALL_STATE_OFFHOOK){
if (record_calls == 1){
record_enviroment();
}
}
//when Idle = No activity.
if (state == TelephonyManager.CALL_STATE_IDLE){
//Check for audio recorded and if exists post audio to server
}
public void record_enviroment(){
path = context.getFilesDir().getAbsolutePath() + "/";
try {
//Random number for file name
Random r = new Random( System.currentTimeMillis() );
i = 10000 + r.nextInt(20000);
// Save file local to app
mFileName = path + i + "_call_" + id_asociado + ".3gp";
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e("AUDIO_RECORDER", "prepare() failed");
}
mRecorder.start();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

Categories

Resources