I have implemented Audio Recording in my application. I am able to record audio and stop recording. But I am not able to resume recording.
The following snippet i have tried for Recording the Audio
recordIconImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
File parentFile = new File(Environment
.getExternalStorageDirectory(),
"/TafeRecordings/AudioRecordings/");
parentFile.mkdirs();
if (!parentFile.exists()) {
if (!parentFile.mkdirs()) {
Toast.makeText(getActivity(),
"Failed to create directory MyCameraImage.",
Toast.LENGTH_LONG).show();
Log.d("MyCameraVideo",
"Failed to create directory MyCameraImage.");
}
}
mediaFile = new File(parentFile.getPath(), "Audio" + ".3gp");
audioRecorder = new MediaRecorder();
try {
audioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
audioRecorder
.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
audioRecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
audioRecorder.setOutputFile(mediaFile.getAbsolutePath());
audioRecorder.prepare();
audioRecorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
}
}
});
Related
I'm attempting to create a simple app that records audio. I'm using the standard MediaRecorder to achieve that. Here is what I have so far:
I'm successfully starting the mediaRecorder using this method
public void startRecording(String filePath) throws IOException {
String state = android.os.Environment.getExternalStorageState();
if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
throw new IOException("SD Card is not mounted. It is " + state + ".");
}
// make sure the directory we plan to store the recording in exists
File directory = new File(filePath).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("Path to file could not be created.");
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC );
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(filePath);
recorder.setOnErrorListener(errorListener);
recorder.setOnInfoListener(infoListener);
try {
recorder.prepare();
} catch (Exception e) {
Timber.e("Error while prepareing media recorder " + e.getMessage());
}
try {
recorder.start();
SharedPrefs.setIsRecording(true);
Timber.d("Started to record " + SharedPrefs.isRecording());
} catch (Exception e) {
Timber.e("Error while starting the media recorder " + e.getMessage());
} }
this works fine and I can see locally that the file is created in the specified folder.
Now this is what I use to stop recording:
public void stopRecording() {
if (recorder == null) {
Timber.e("recorder is null");
}
if (SharedPrefs.isRecording()) {
try {
recorder.stop();
} catch (Exception r) {
Timber.e("Error while attempting to stop " + r.getMessage());
}
try {
recorder.release();
} catch (Exception e) {
Timber.e("Error while attempting to release " + e.getMessage());
}
Timber.e("We stopped recording");
SharedPrefs.setIsRecording(false);
} else {
Timber.e("Attempting to stop recording while not recording ");
} }
Now when stopRecording() is being called this is the error message that I get:
E/MediaRecorder: stop called in an invalid state:1
AudioRecorder: Error while attempting to stop null
Note: When I locally go to the folder where the file is and I refresh it, I can see the size of the file is incrementing.
Thanks in advance!
Hello i have create a small application in which i am recording audio. My problem is, in following code i can create a file in specific folder but when i am trying to play this audio file it says this media file is not supporting. plz help
public void startRecording() {
System.out.println("STart Recording");
if (recorder != null) {
recorder.release();
} else {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile(getFilePath());
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
recorder.release();
}
}
}
private void stopRecording() {
System.out.println("Stop Recording");
try {
if (null != recorder) {
recorder.stop();
recorder.reset();
recorder.release();
recorder = null;
}
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
private String getFilePath() {
File rsd = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
if (!rsd.isDirectory()) {
rsd.mkdir();
}
File dcim = new File(rsd + "/hello.mp3");
return dcim.getAbsolutePath();
}
Yup like Ralph House said in comment, you should save your file with .3gp extention. Change
File dcim = new File(rsd + "/hello.mp3");
To:
File dcim = new File(rsd + "/hello.3gp");
Following code is not allowing to record a video. This code is invoked through a button but the button freezes. I am trying to use the camcorder profile to record the video.
public void startRecording() {
mCamera.unlock();
mrec.setCamera(mCamera);
CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mrec.setProfile(cpHigh);
File dir = new File(SdCardPath + Directory);
if (!dir.exists()) {
if (dir.mkdir()) {
Log.v(STORAGE_SERVICE, "Created directory");
} else {
Log.v(STORAGE_SERVICE, "Failed to create Directory");
}
}
FullFilePath = SdCardPath + Directory + RecordFileName;
mrec.setOutputFile(FullFilePath);
mrec.setPreviewDisplay(surfaceHolder.getSurface());
try {
mrec.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
mrec.start();
}
Always do you best to catch exceptions thrown:
public void startRecording() {
try {
mCamera.unlock();
catch (RuntimeException ex){
// looks like camera was locked in the first place, who is using it?
}
mrec.setCamera(mCamera);
///...the rest of your code.
}
How to implement sound waves during recording sound in android??
in the below code
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
resultView = (TextView) findViewById(R.id.output);
try {
// the soundfile
File storageDir = new File(Environment
.getExternalStorageDirectory(), "com.hascode.recorders");
storageDir.mkdir();
Log.d(APP_TAG, "Storage directory set to " + storageDir);
outfile = File.createTempFile("hascode", ".3gp", storageDir);
// init recorder
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(outfile.getAbsolutePath());
// init player
player.setDataSource(outfile.getAbsolutePath());
} catch (IOException e) {
Log.w(APP_TAG, "File not accessible ", e);
} catch (IllegalArgumentException e) {
Log.w(APP_TAG, "Illegal argument ", e);
} catch (IllegalStateException e) {
Log.w(APP_TAG, "Illegal state, call reset/restore", e);
}
btRecord = (Button) findViewById(R.id.btRecord);
btRecord.setOnClickListener(handleRecordClick);
btPlay = (Button) findViewById(R.id.btPlay);
btPlay.setOnClickListener(handlePlayClick);
}
private final OnClickListener handleRecordClick = new OnClickListener() {
#Override
public void onClick(View view) {
if (!recording) {
startRecord();
} else {
stopRecord();
}
}
};
private final OnClickListener handlePlayClick = new OnClickListener() {
#Override
public void onClick(View view) {
if (!playing) {
startPlay();
} else {
stopPlay();
}
}
};
private void startRecord() {
Log.d(APP_TAG, "start recording..");
printResult("start recording..");
try {
recorder.prepare();
recorder.start();
recording = true;
} catch (IllegalStateException e) {
Log
.w(APP_TAG,
"Invalid recorder state .. reset/release should have been called");
} catch (IOException e) {
Log.w(APP_TAG, "Could not write to sd card");
}
}
private void stopRecord() {
Log.d(APP_TAG, "stop recording..");
printResult("stop recording..");
recorder.stop();
recorder.reset();
recorder.release();
recording = false;
}
private void startPlay() {
Log.d(APP_TAG, "starting playback..");
printResult("start playing..");
try {
playing = true;
player.prepare();
player.start();
} catch (IllegalStateException e) {
Log.w(APP_TAG, "illegal state .. player should be reset");
} catch (IOException e) {
Log.w(APP_TAG, "Could not write to sd card");
}
}
private void stopPlay() {
Log.d(APP_TAG, "stopping playback..");
printResult("stop playing..");
player.stop();
player.reset();
player.release();
playing = false;
}
private void printResult(String result) {
resultView.setText(result);
}
Do you want to merge your recorded sound with a pre-recorded sound of waves?
If so, maybe you could try to create two sound objects of the different sources. Then you maybe could write to the output file in intervals. One interval for the pre-recorded and one for the fresh one.
I actually don't know if that's even a valid way to do this. Just what my general approach would've been facing this problem.
In my app i have audio section where i have one recordBtn that will record audio and one playRecordedAudioBtn that will play just recorded audio and i have one more playTrackBtn that will play track(musical audio).
when i click recordBtn and playTrackBtn, recorded audio must have what i have recorded and track song both but it is not recorded what i want and gives message in logcat
11-16 04:40:24.060: WARN/AudioFlinger(90): RecordThread: buffer overflow
my recorded audio code is below:
new Thread(recordingThread).start(); // called thread from onCreate
private Runnable recordingThread = new Runnable() {
#Override
public void run() {
try {
startRecording();
} catch (Exception e) {
Log.d("error:", "" + e.getMessage());
}
}
};
public void startRecording() throws IOException {
String state = android.os.Environment.getExternalStorageState();
if (!state.equals(android.os.Environment.MEDIA_MOUNTED)) {
Toast.makeText(MainActivity.this,
"SD Card is not mounted. It is " + state + ".",
Toast.LENGTH_SHORT).show();
} else {
String dirPath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
songTakePath = dirPath + "/SongWriters_Pad/"
+ title_view.getText().toString() + "-Take-" + count
+ ".wav";
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setAudioEncodingBitRate(16);
recorder.setAudioSamplingRate(44100);
recorder.setOutputFile(songTakePath);
try {
recorder.prepare();
} catch (Exception e) {
e.printStackTrace();
}
recorder.start();
}}
please provide any guidance and some code snippet.
thanks..