Android: Pause voice Recorder and Resume - android

I've used following code as base to create a recorder. I can start and stop audio recording and it gets saved properly in the location. But now I have a requirments to pause the voice recorder
How to pause the audio recorder? And resume voice recording? I've seen a voice recording appliation in my samsung galaxy Ace, it has a pause button.
Can someone enlighten me.
public class audio {
final MediaRecorder recorder = new MediaRecorder();
final String path;
/**
* Creates a new audio recording at the given path (relative to root of SD card).
*/
public audio(String path) {
this.path = sanitizePath(path);
}
private String sanitizePath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.contains(".")) {
path += ".3gpp";
}
return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
}
/**
* Starts a new recording.
*/
public void start() 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(path).getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
throw new IOException("Path to file could not be created.");
}
try {
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
/**
* Stops a recording that has been previously started.
*/
public void stop() throws IOException {
recorder.stop();
recorder.reset();
recorder.release();
}
public void pause() {
}
}

check out these 2 pages
http://developer.android.com/guide/topics/media/index.html
http://developer.android.com/reference/android/media/MediaRecorder.html
according to the first article there is a pause()
but i dont see a pause() method on that second link so im not sure
the other thing that the first article references:
"When you call stop(), however, notice that you cannot call start() again until you prepare the MediaPlayer again.
Always keep the state diagram in mind when writing code that interacts with a MediaPlayer object, because calling its methods from the wrong state is a common cause of bugs."
so maybe u can just stop() prepare mediaplayer then start() again

Related

MediaRecorder does not record audio in the background

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?

Sending android audio recorded file as an attachment of an email giving error when trying to read file (URI path not absolute)

I'm saving an audio file using Media recorder and the following code:
public class AudioRecorder {
final MediaRecorder recorder = new MediaRecorder();
final String path;
/**
* Creates a new audio recording at the given path
*/
public AudioRecorder(String path) {
this.path = sanitizePath(path);
}
public static String sanitizePath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if(path.endsWith("/")){
path = path + "/";
}
return path;
}
/**
* Starts a new recording.
*/
public void start() throws IOException {
// make sure the directory we plan to store the recording in exists
File directory = new File(path).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(path);
recorder.prepare();
recorder.start();
}
/**
* Stops a recording that has been previously started.
*/
public void stop() throws IOException {
recorder.stop();
recorder.release();
}
}
I am then calling this class using the following code
int timeOfRecroding = AppPrefs.getSettingsAdditionalTimeOfRecording() * 60 * 1000;
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File directory = cw.getDir("media", Context.MODE_PRIVATE);
final String pathAndName = AudioRecorder.sanitizePath(directory.getAbsolutePath() + "/LocRec.3gp");
final AudioRecorder audioRecorder = new AudioRecorder(pathAndName);
if(Constants.isTest){
showToast("Starting recording for [" + AppPrefs.getSettingsAdditionalTimeOfRecording() + "] minutes");
showToast("Recording to path: [" + pathAndName + "]");
}
and then of course using
audioRecorder.start();
and
audioRecorder.stop();
do the actual recording
After the recording is done I using the same pathAndName to get the file and send it as attachment in an email using the following code to get the file
new File(new URI(AppPrefs.getInfoToSend(Constants.SERVICE_CODE_SEND_RECORDING, Constants.MESSAGE_TYPE_EMAIL)))
but this is throwing an excpetion
URI is not absolute: /data/data/com.testrecoding.record/app_media/LocRec.3gp
I appreciate any help,
Thanks,
Wassim
it may sound stupid, but I found a turn-around, not really a turn-around, not sure how I missed it in the first place, getting the file from the path directly without using URI
new File("PathOfFIle")

Android MediaRecorder IllegalStateException

I am making an application that allows a user to record audio and save it somewhere in the SD card. I am using a MediaRecorder to do record the audio.
I am reusing some of the code from the androiddevblog website as it was recommended by another user on stackoverflow to check those tutorials.
My problem is whenever I click the button to record audio I get an error saying "Your Application has been forced to stop". I used the debugger to find out that it is because I have an illegalStateException on recorder.stop()
I am aware that nothing will get recorded based on my code. I want to first make sure the file gets created and saved.
I posted this question before, and solved my original problem but ran into this one after. So it may seem as a duplicate.
public class MyRecorderActivity extends Activity{
private static final String AUDIO_RECORDER_FOLDER = "AudioRecorder";
private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp"
private Button audio;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.questionandanswer);
....
....
audio = (Button) findViewById(R.id.audio_recordactivity);
audio.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startRecording();
}
});
}
private String getFilename(){
String filepath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filepath,AUDIO_RECORDER_FOLDER);
if(!file.exists()){
file.mkdirs();
}
return (file.getAbsolutePath()+ "/ " + System.currentTimeMillis() + AUDIO_RECORDER_FILE_EXT_3GP);
}
private void startRecording(){
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(getFilename());
try {
recorder.prepare();
recorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
recorder.stop();
recorder.reset();
recorder.release();
}
}
Make sure to include following permission.
<uses-permission android:name="android.permission.RECORD_AUDIO" />
May be another recording is active(May be a default recorder or recorderr of another downloaded application)stop that recording and do the operation again, hope it works

Thick slow recorded sound using android recorder

I'm developing an android application that records sound using Mediarecorder class
The following is a part from my code:
public void start() 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(path).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.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setAudioEncodingBitRate(16);
recorder.setAudioSamplingRate(44100);
try {
recorder.setOutputFile(path);
} catch (IllegalStateException e) {
e.printStackTrace();
}
recorder.prepare();
recorder.start();
}
when the recorder stops , i played it using mediaplayer class but the result sound is very thick and slow .. what could be the problem?
As per the MediaRecorder documentation the sampling rate for AMRNB is 8kHZ and you are setting this to a different value. I suspect this as the issue.
Can you comment these lines and check if that works for you:
//recorder.setAudioEncodingBitRate(16);
//recorder.setAudioSamplingRate(44100);

How do i enable media option on android emulator with command line

in my app i am recording speech so that i need to set up my emulator able to record speech.
i have searched in the google i got some solution that is need to start emulator by manually with media option. i use the following cmd but i got error.
emulator -avd Test -audio-in MIC
i am using Android 2.2(Api 2.2) on windows 7. How do i enable MIC option on my emulator. please help me.
I got the following error:
>emulator -avd Test -audio-in MIC
>unknown option: -audio-in
please use -help for a list of valid options
Try to use this example:
package com.benmccann.android.hello;
import java.io.File;
import java.io.IOException;
import android.media.MediaRecorder;
import android.os.Environment;
/**
* #author Ben McCann
*/
public class AudioRecorder {
final MediaRecorder recorder = new MediaRecorder();
final String path;
/**
* Creates a new audio recording at the given path (relative to root of SD card).
*/
public AudioRecorder(String path) {
this.path = sanitizePath(path);
}
private String sanitizePath(String path) {
if (!path.startsWith("/")) {
path = "/" + path;
}
if (!path.contains(".")) {
path += ".3gp";
}
return Environment.getExternalStorageDirectory().getAbsolutePath() + path;
}
/**
* Starts a new recording.
*/
public void start() 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(path).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(path);
recorder.prepare();
recorder.start();
}
/**
* Stops a recording that has been previously started.
*/
public void stop() throws IOException {
recorder.stop();
recorder.release();
}
}
Hope this help you. Let us know how it goes, or if you need further help.

Categories

Resources