Outgoing call recording audio file has no sound - java

After recording an outgoing phone call, I am trying to play the recorded file - to make sure the call recording worked as expected (I am doing it using 'MediaPlayer'), but there is no sound.
So I tried to access the actual file on the phone (simply attached the phone to the computer and accessed it's files). When I played it the recording it was in the right length but again no sound.
What am I missing?
This is how I record the phone call:
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
File callAudioFile = null;
try {
File downloadsDir = context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
callAudioFile = File.createTempFile("deTuCelRecord", ".amr", downloadsDir);
} catch (IOException e) {
e.printStackTrace();
}
assert callAudioFile != null;
audioFilePath = callAudioFile.getAbsolutePath();
recorder.setOutputFile(audioFilePath);
try {
recorder.setOnErrorListener(new MediaRecorder.OnErrorListener() {
#Override
public void onError(MediaRecorder mr, int what, int extra) {
Log.e("MediaRecorder", "MediaRecorder error " + what + " " + extra);
}
});
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
recorder.start();
This is the code which ends the call recording:
recorder.stop();
recorder.release();
This is how I play the audio file:
MediaPlayer mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(audioFilePath);
mPlayer.prepare();
Toast.makeText(getApplicationContext(), "PLAYING AUDIO", Toast.LENGTH_LONG).show();
mPlayer.start();
Log.d("PLAY_AUDIO", "Started playing audio");
} catch (IOException e) {
Log.e("PLAY_AUDIO", "Failed to play audio");
}

Please check this Accessibilty Service in your testing phone.
If you are trying to record call on Android Q.
Please refer this link
You can try
recorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL); It need Manifest.permission.CAPTURE_AUDIO_OUTPUT permission.
Please check AudioSource Source documentation for difference between VOICE_CALL and VOICE_COMMUNICATION

The issue was the android version - from android 10 it didn't allow me to record the call but on android 9 it did.

Related

Can not play music from http server for over 3 seconds

I am trying to make a music streaming app. For the music I use a URL, and for some reason, the music is playing for 3 seconds and then automatically stops. pls help me fix it.
I also get a message in the Run says: "MediaPlayer finalized without being released".
Thanks.
the code:
String url = "https://radio.streamgates.net/stream/1036kh"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
try {
mediaPlayer.setDataSource(url);
} catch (IOException e) {
Toast.makeText(this,"failed", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
try {
mediaPlayer.prepare(); // might take long! (for buffering, etc)
} catch (IOException e) {
Toast.makeText(this,"failed", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
mediaPlayer.start();

Media Recorder start failed in android for front camera for Samsung Galaxy s3

I am working on video recording app in which i want to display preview and when user click on record button it start recording and when user click stop button it stop recording.
I got camera preview and recording back camera is working fine.
But when I flip camera to front camera and when I start recording it occurs error like this:
FATAL EXCEPTION: main java.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method) at
com.opkix.app.fragments.CameraFragment.startRecording(
CameraFragment.java:104)
Here's my code for recording video code:
private boolean prepareMediaRecorder() {
// set the orientation here to enable portrait recording.
mediaRecorder = new MediaRecorder();
mCamera.unlock();
mediaRecorder.setCamera(mCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setOutputFile(StorageUtils.getOutputMediaFilePath());
mediaRecorder.setMaxDuration(120000); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(100000000); // Set max file size 50Mb
mediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}
Can anyone please suggest solution?
now, i am also working on Video Recording App.Please one time Run your Code in some another device's.also i am sharing my Code with you.in which some of code is my App related code.remove that if you don't need that.
My Code :
private boolean prepareVideoRecorder() {
mRecorder = new MediaRecorder();
// Both are required for Portrait Video
mCamera.setDisplayOrientation(90);
if (mCameraId == CAMERA_FACING_FRONT) {
mRecorder.setOrientationHint(270);
} else {
mRecorder.setOrientationHint(90);
}
// Step 1: Unlock and set camera to MediaRecorder
mCamera.unlock();
mRecorder.setCamera(mCamera);
// Step 2: Set sources
mRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
mRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_480P));
// Step 4: Set output file
final File folder;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
folder = new File(Environment.getExternalStorageDirectory() + "/CameraApp/Videos");
} else {
folder = new File(Environment.getExternalStorageDirectory() + "/CameraApp/Videos");
}
boolean success = true;
File videoFile;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (success) {
videoFile = new File(folder.getAbsolutePath() + File.separator + getFileNameCustomFormat() + " " + ".mp4");
SavedVideoPath = getFileNameCustomFormat() + " " + ".mp4";
Log.e("Video Path - ", SavedVideoPath);
} else {
Toast.makeText(getBaseContext(), "Video Not saved", Toast.LENGTH_SHORT).show();
return true;
}
mRecorder.setOutputFile(String.valueOf(videoFile));
// mRecorder.setVideoSize(mPreviewWidth, mPreviewHeight);
// Step 5: Set the preview output
mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
// Step 6: Prepare configured MediaRecorder
try {
mRecorder.prepare();
} catch (IllegalStateException e) {
Toast.makeText(getApplicationContext(), "prepareVideoRecorder() Exception: " + e.getMessage(), Toast.LENGTH_LONG).show();
releaseMediaRecorder();
return false;
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "prepareVideoRecorder() Exception: " + e.getMessage(), Toast.LENGTH_LONG).show();
releaseMediaRecorder();
return false;
}
return true;
}
And let me know what happen.? after trying my code.
Hope this will Helps :

Media Playback of Recorded Audio not able to play on speaker on Android

I am making a Call recorder app in Android. I have a service that detects the incoming and outgoing call and as soon as the service detects the incoming or outgoing call it starts the recording through media player. It is getting recorded sucessfully but I am unable to play it on speaker. The volume is very low even after enabling speakers through Audio Manager. Please let me know where I might be going wrong.
Note: I am guessing that my audio is not getting recorded properly because if I play the recorded file through my phone music player, then even there I am not able to play that file on speaker.
below is my code which is used to start the recording:
try{
MediaRecorder mediaRecorder = new MediaRecorder();
if(AUDIO_SOURCE_TYPE.equalsIgnoreCase("VOICE_COMMUNICATION"))
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_COMMUNICATION);
}
else if(AUDIO_SOURCE_TYPE.equalsIgnoreCase("VOICE_UPLINK"))
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_UPLINK);
}
else if(AUDIO_SOURCE_TYPE.equalsIgnoreCase("VOICE_DOWNLINK"))
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_DOWNLINK);
}
else if(AUDIO_SOURCE_TYPE.equalsIgnoreCase("MIC"))
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
}
else if(AUDIO_SOURCE_TYPE.equalsIgnoreCase("DEFAULT"))
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
}
else
{
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.VOICE_CALL);
}
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setOutputFile(getTempFilename());
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.prepare();
} catch (Exception e) {
e.printStackTrace();
}
mediaRecorder.start();
Below is the Media Playback Code :
final MediaPlayer mp=new MediaPlayer();
String uri= getIntent().getStringExtra("fileURI"); //fetches the string defining the path of file to be played
Uri fileURI = Uri.parse(uri);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setSpeakerphoneOn(true);
int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVolume, AudioManager.FLAG_PLAY_SOUND);
try{
try {
mp.setDataSource(getApplicationContext(), fileURI);
} catch (IOException e) {
e.printStackTrace();
}
}
mp.prepare();
}catch(Exception e){e.printStackTrace();}
mp.start();

Android Force Close issue of Video Recorder implementation in Custom Camera app

I am developing an Android Custom Camera app and trying to implement the video recorder feature in it. And I am using the below code
private boolean prepareMediaRecorder()
{
myCamera = getCameraInstance();
mediaRecorder = new MediaRecorder();
myCamera.unlock();
mediaRecorder.setCamera(myCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface());
try
{
File newFile = File.createTempFile("videocapture", ".mp4", Environment.getExternalStorageDirectory());
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
mediaRecorder.setOutputFile(newFile.getAbsolutePath());
}
catch (IOException e)
{
e.printStackTrace();
finish();
}
try
{
mediaRecorder.prepare();
}
catch (IllegalStateException e)
{
releaseMediaRecorder();
return false;
}
catch (IOException e)
{
releaseMediaRecorder();
return false;
}
return true;
}
But if i use the same code and run it on Galaxy tab, i get a force close at this location of code
mediaRecorder.start();
And the error log looks like this.
http://textuploader.com/?p=6&id=ngcFk
The code works just fine on the same, when i change
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH));
to
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
Not getting why is it so. Please Help! Thanks!
You should check whether the profile exists. I think for tab it is not supported. You can try that with hasprofile

File that I recorded with MediaRecorder can't be played

I'm using MediaRecoder to record sound, but after I'm done recording it can't be played. I tried with Google Play Music, ES Media Player, even uploaded it to pc and tried opening it with Winamp. Nothing plays it!
//AUDIO RECORDER
recorder = new MediaRecorder();
recorder.reset();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
externalOutputPath = externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3";
recorder.setOutputFile(externalOutputPath);
}
else
{
storagePath = Environment.getDataDirectory().getAbsolutePath();
recorder.setOutputFile(storagePath + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3");
}
Even tried opening it inside the application with button click:
public void testPlay (View v) throws IllegalArgumentException, SecurityException, IllegalStateException, IOException{
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(externalOutputPath);
mediaPlayer.prepare();
mediaPlayer.start();
}
But that crashes the application. But that is not the main problem, the main problem is the fact that there's no way I can play the file. Is there something wrong with encoding or something?
I also tried to change it from .mp3 to .3gp and it didn't work. Then I also tried removing .mp3 and .3gp, leaving just 'test' as a name and then it didn't even recognize it as an audio file.
Oh, and if anyone wants the logcat when the application crashes:
07-31 16:51:43.953: E/AndroidRuntime(26918): java.lang.IllegalStateException: Could not execute method of the activity
07-31 16:51:43.953: E/AndroidRuntime(26918): Caused by: java.lang.reflect.InvocationTargetException
07-31 16:51:43.953: E/AndroidRuntime(26918): Caused by: java.io.IOException: setDataSourceFD failed.: status=0x80000000
But again, app crashing isn't the problem at the moment. First I want to solve the problem why the audio file can't be played. But if you have any idea why it crashes I would also appreciate it!
I'm still not sure what was the problem, but I think there was something wrong with the way I stopped recording (the whole try, catch thing). I just rewrote the whole code and put the MediaRecorder in two different methods. startRecording() and stopRecording() method. And now it works perfectly!
startRecording()
public void startRecording (){
recorder = new MediaRecorder();
recorder.reset();
recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
externalStoragePath = Environment.getExternalStorageDirectory().getAbsolutePath();
externalOutputPath = externalStoragePath + File.separator + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3";
recorder.setOutputFile(externalOutputPath);
}
else
{
storagePath = Environment.getDataDirectory().getAbsolutePath();
recorder.setOutputFile(storagePath + "/Android/data/com.whizzappseasyvoicenotepad/test.mp3");
}
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
stopRecording()
public void stopRecording() {
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
}

Categories

Resources