Android Record Audio issue - android

I found the code to record audio, but I always get:
The method setOutputFile(FileDescriptor) in the type MediaRecorder is not applicable for the arguments (Uri)
So how would I need to describe the filepath that it works?
// Prepare recorder source and type
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// File to which audio should be recorded
File outputFile = getFileStreamPath("output.amr");
Uri target = Uri.parse(outputFile.getAbsolutePath());
recorder.setOutputFile(target);
// Get ready!
recorder.prepare();
// Start recording
recorder.start();
// Stop and tidy up
recorder.stop();
recorder.release();

You're trying to pass in an Uri as parameter to the method which doesn't expect that.
Just replace recorder.setOutputFile(target) with:
recorder.setOutputFile(outputFile.getAbsolutePath());
That should work since string parameter is allowed.

Related

How can I create a microphone app on Android?

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!

How to set mimetype of audio file, so that it work on both android and ios?

I'm currently recording audio with the following code:
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
audio_path = getFilename();
recorder.setOutputFile(audio_path);
recorder.prepare();
recorder.start();
It is working, and I can record an audio with the mime type audio/mpeg. But when I send this audio file to server or to an IOS device then they are not able to play this audio. It is working on other android devices. On FireFox it shows error message that No video with supported format or mime type found and on chrome it does also not work.
I tried to change the mime type with following code, which I found on
This Link:
mRecorder.stop();
mRecorder.release();
String[] paths = {mFileName};
String[] mimeTypes = {"audio/mp3"};
MediaScannerConnection.scanFile(getApplicationContext(),
paths,
mimeTypes,
new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
System.out.println("SCAN COMPLETED: " + path);
}
});
But it is also not changing the mime type of the audio file. How I can set the mime type audio/mp3?
AAC format worked in both iOS and Android set MediaRecorder Encoder to AAC
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
Check the below link there is multiple answer
Link1
Link2

How to Store the Recorded Audio Files in a Folder

Can anyone please explain how to store the recorded Audio files in a separate folder?
I am using default recorder for recording audio files
Intent = new Intent (MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, 2);
and I am getting the absolute audio path:
absolutepath audiopath is : /mnt/sdcard/recording1225555579.3gpp
You can try by specifying the path yourself.
private void beginrec() throws IllegalStateException, IOException{
ditchMediaRecorder();
recorder=new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/sdcard/voice/"); //like this
recorder.prepare();
recorder.start();
}

audio recording in android

I have and android emulator and microphone connected to my pc. I want to capture pcm pulses from microphone (i.e. record voice) and then send to udp socket. please anybody help me in source code at least for voice recording.
You can use this code for your audio recording:
MediaRecorder recorder;
void startRecording() throws IOException
{
SimpleDateFormat timeStampFormat = new SimpleDateFormat(
"yyyy-MM-dd-HH.mm.ss");
String fileName = "audio_" + timeStampFormat.format(new Date())
+ ".mp4";
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/sdcard/"+fileName);
recorder.prepare();
recorder.start();
}
protected void stopRecording() {
recorder.stop();
recorder.release();
}
Check Audalyzer, a sample application showing you how to read the raw audio stream from the microphone on real time.

Record audio in Android

How to put on pause? I have not found such a function. There are other ways?
You can try MediaRecorder class available in Android. It has options to start and stop your recording
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(PATH_NAME);
recorder.prepare();
recorder.start(); // Recording is now started
...
recorder.stop();
recorder.reset(); // You can reuse the object by going back to setAudioSource() step
recorder.release(); // Now the object cannot be reused
https://developer.android.com/reference/android/media/MediaRecorder.html

Categories

Resources