I'm trying to record audio using MediaRecorder on the Droid X2, and I'm running into issues. The MediaRecorder seems to prepare and start recording just fine, but when I stop recording and try to listen to the file that is produced the playback immediately stops. Even if I try to open the audio file in the standard media player app, it immediately stops.
Here's the code I'm using to record:
mCurrentRecordingFilePath = mContext.getExternalFilesDir(null).getAbsolutePath() + File.separator + fileName;
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
mRecorder.setOutputFile(mCurrentRecordingFilePath);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
// mRecorder.setAudioEncodingBitRate(16);
// mRecorder.setAudioSamplingRate(44100);
try {
mRecorder.prepare();
mIsPrepared = true;
} catch (IOException e) {
Log.e("Test", "MediaRecorder prepare() failed");
e.printStackTrace();
}
if (mRecorder != null && mIsPrepared) {
mRecorder.start();
mIsRecording = true;
Log.i("Test", "Started audio capture: " + mCurrentRecordingFilePath);
}
I commented out the setAudioEncodingBitRate() call because it was causing the prepare() to fail, and just to be on the safe side I also commented out setAudioSamplingRate(). I have tried every combination of output format and audio encoder that is available and the result is always the same. I get no exceptions, and a file is created that is not empty, but it will not play back properly.
Not sure if it will help diagnose, but here's the code I use to stop recording:
if (mRecorder != null && mIsRecording) {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
mIsPrepared = false;
mIsRecording = false;
Log.i("Test", "Stopped audio capture");
}
The recording code works fine on a Galaxy Nexus, Nexus S, EVO 4G, and Galaxy SII. Any idea why my audio file would be bad on the Droid X2?
Found the answer here: MediaPlayer cant play audio files from program data folder?
Problem wasn't the recording, it was the way I was setting the MediaPlayer's datasource. Though I have no idea why the stock media player app wouldn't play the file... maybe it's a permission thing. Here's the code I was using to play the file:
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource(fileName);
mPlayer.prepare();
mPlayer.start();
Here's the code that will work if the file is stored in the external files directory:
mPlayer = new MediaPlayer();
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.setDataSource((new FileInputStream(fileName)).getFD());
mPlayer.prepare();
mPlayer.start();
Related
I want to create a microphone app on Android that will receive sound through the microphone and play through the speakerphone but I don't know exactly what classes and services I should use.
The core of your answer is to:
A) record and store, as stated here.
MediaRecorder recorder = new MediaRecorder();
String status = Environment.getExternalStorageState();
if(status.equals("mounted")){
String path = Environment.getExternalStorageDirectory().toString()+"/YOURFOLDER"; // your custom path
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); // notice that this is the audio format, and you might want to change it to [other available audio formats][2]
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
// To start recording
recorder.start();
// To stop recording
recorder.stop();
recorder.release();
} else {
// Handle the situation
}
[Other available audio formats | 2]
B) Get recordings, as stated partially here. You should then show them.
try {
String path = Environment.getExternalStorageDirectory().toString()+"/YOURFOLDER"; // your custom path
File directory = new File(path);
File[] files = directory.listFiles();
} catch {
// Handle errors (or maybe no files in the directory)
}
C) Play recordings, as stated partially here.
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(Environment.getExternalStorageDirectory().toString()+"/YOURFOLDER"+"/yourfilename.formatextension"; // your custom pathare to use the file format and directory you used when saving
mp.prepare();
mp.start();
Hope this answer helps!
We call the startRecord method from the Firebase JobService. When our application is in the foreground, sound recording from the microphone passes without problems. However, if the application is in the background (no foreground activity), the MediaRecorder records silence. The recording also happens without problems if you call startRecord from the foreground service, however, our application must make a hidden recording. Here is the startRecord method:
void startRecord(int duration) {
audioFile = Environment.getExternalStorageDirectory() + "/record.amr";
try {
File outFile = new File(audioFile);
if (outFile.exists()) {
outFile.delete();
}
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(audioFile);
mediaRecorder.prepare();
mediaRecorder.start();
//start timer for stop recording
handler.postDelayed(StopRecordTask, duration * 1000);
Log.d(LOG_TAG, "Audio record start");
} catch (Exception e) {
if (mediaRecorder != null) {
mediaRecorder.release();
mediaRecorder = null;
}
errorMessage();
e.printStackTrace();
}
}
We use android SDK version 26. Tell me, please, what is the cause of the problem?
This is the function called in a service for call recording.
try{
mediaRecorder = new MediaRecorder();
file = File.createTempFile("" + cal.getTime(), ".amr", fileDirPath);
this.phoneCall.setPathToRecording(file.getAbsolutePath());
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
mediaRecorder.setAudioChannels(1);
mediaRecorder.setAudioSamplingRate(8000);
mediaRecorder.setAudioEncodingBitRate(12200);
mediaRecorder.setOutputFormat(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(file.getAbsolutePath());
mediaRecorder.prepare();
mediaRecorder.start();
} catch (Exception e) {
e.printStackTrace();
}
Tested with SAMSUNG, LENOVO, GIONEE Works fine.
The problem occurs in the MOTOROLA and MI devices
What code is need to be change to get proper other person sound in the recoded file
I am trying to find out if my device is recording audio correctly (Volume of recorded audio is not too low and actually the recorded file has sound). The way I tried doing it is:
start recording --> play sound --> stop recording --> get file recorded max volume
The code I used to record sound:
public void playSound() {
File myDataPath = new File(getActivity().getFilesDir().getAbsolutePath()
+ File.separator + ".CheckAudio");
if (!myDataPath.exists())
myDataPath.mkdirs();
recordFile = myDataPath + File.separator + "Recording_" + new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()) + ".mp3";
am.setStreamVolume(AudioManager.STREAM_RING, am.getStreamMaxVolume(AudioManager.STREAM_RING), 0);
am.setStreamVolume(AudioManager.STREAM_NOTIFICATION, am.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION), 0);
Uri defaultRingtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
try {
md = new MediaRecorder();
md.setAudioSource(MediaRecorder.AudioSource.MIC);
md.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
md.setOutputFile(recordFile);
md.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
md.prepare();
md.start();
} catch (IllegalStateException | IOException e) {
recording = false;
removeItem("Unable to record audio, please try again."); // (Show toast)
return;
}
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(getActivity(), defaultRingtoneUri);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
mediaPlayer.prepare();
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
md.stop();
md.release();
mediaPlayer.release();
mediaPlayer = null;
// get recordfile volume
}
});
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
removeItem("Unable to play audio");
sound = false;
}
}
However, I can't find out how to analyze the mp3 file created and check if it is not empty (from sound), is there a library or another way?
I hope you guys understood what I am trying to achieve as my English is pretty bad, Thanks.
EDIT:(Some more explaination)
If you play sound (ringtone or something) while recording sound from microphone, the decibels recorded should be around 90 decibels. meaning the sound playing working and also the microphone, but if the decibels recorded around 30 means only microphone is working, and playing sound not, if the decibels are around zero then the microphone is not working.
You can use a visualiser to visualise real time if recording sound is getting too low or too loud.
I have build a project which visualise recording sound strength via bar graph . Higher the bar louder the recorded sound lower the bar low decibels .
This project also have inapp player which allow user to play all his recordings. The inbuilt player also visualise playback sound data.
I am suggesting this because I thought this is what you are trying to achieve in
start recording --> play sound --> stop recording --> get file recorded max volume.
Instead of getting max volume each time you can rely on visualiser to keep an eye on recorder if recording file is getting recorded above acceptable decibals.
You can find source code on github
https://github.com/hiteshsahu/Android-Audio-Recorder-Visualization-Master
How can i play audio from a specified time ?
I have a prerecorded audio and i want to play it from a specific time to a specific time.
I have tried seekTo function in MediaPlayer but it doesnt seem to work.
mPlayer = new MediaPlayer();
try{
mPlayer.setDataSource(PATH_FILE + selectedAudio);
mPlayer.prepare();
mPlayer.seekTo(2);
mPlayer.start();
}catch(IOException ioe){
System.out.println("Error in startPlaying method");
}
Thanks in advance