Android audio capture - android

I'm following this article to capture Audio on Android. I thought it will be easy, but there's a problem. Here's the code:
File dir = activity.getDir("recordings", Context.MODE_PRIVATE);
File file = new File(dir, "testaudio.mpeg4");
FileUtil fileUtil = new FileUtil();
File parent = file.getParentFile();
if(!parent.exists()){
Log.i(TAG, "Creating non-existent path:" + parent.getAbsolutePath());
parent.mkdirs();
}
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
try{
recorder.setOutputFile(file.getAbsolutePath());
recorder.prepare();
recorder.start();
}catch(IOException e){
//more code
}
So, it does create a testaudio.mpeg4 file under /data/data/com.myapp/app_recordings/ folder. But, after transferring the file to Mac (using adb pull) it doesn't play. I am on a Mac and I've tried a few audio formats (e.g. MP3, MPEG, 3GP etc.) but nothing seems to be working. Any help/guidance will be appreciated.

Use this
File dir = new File(Environment
.getExternalStorageDirectory().getAbsolutePath() + "/recording/");

It seems like a Mac issue, nothing wrong with the code or device. I can hear the Audio on the device.

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

Illegal State Exception on Media Recorder

I want to use Media Recorder to record and save an audio file to the sd card. The debugger has shown that the line recorder.start() raises an IllegalStateException. The code is taken from the android dev website and the only change is to the file name and path.
When I reach the error in the debugging menu, I am shown the View class which says:
Source not found.
The source attachment does not contain the source for the file View.class.
You can change the source attachment by clicking Change Attached Source below:
canRecord is a boolean set initially to true, which dictates which method is called in an onClick function. That functionality is working.
#SuppressLint("SimpleDateFormat")
private void record(){
SimpleDateFormat timeStampFormat = new SimpleDateFormat("MM/dd/yyyy");
String audio_path = Environment.getExternalStorageDirectory() + "/resources/resources/WI1/";
String fileName = username +"-"+ timeStampFormat.format(new Date())+".mp4";
audio_button.setText("Recording");
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(audio_path+fileName);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
recorder.prepare();
} catch (IOException e) {
Toast.makeText(this, "Can't record audio at this time", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
canRecord = false;
recorder.start();
} // record
This is the stop recording function, although it has never reached this point.
private void stopRecording(){
audio_button.setText("Attach Audio");
recorder.stop();
recorder.release();
}
Finally, I have added the following permissions to the manifest:
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
How can I solve this? Thanks!
May be there is problem in filename you are giving to the recorder.Try this:
String audio_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/resources/resources/WI1/";
String fileName = audiopath+"-"+ timeStampFormat.format(new Date())+".mp4";
May it helps..
I am using libgdx(multiplatform open source game engine) and i had a similar problem when i was trying to use androids code instead of the libgdx code for recording.Turned out i needed to dispose of my libgdx recorder code or just delete it all.So perhaps you have another recorder interfering somewhere

Recorded Audio using Native Audio Recorder Issue

I am recording audio using Audio recorder (native app) of android.Using following code :
final Intent audioIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
String path = Environment.getExternalStorageDirectory()
.toString();
File AudioFolder = new File(path + "/MyFolder/",
"RecordedAudio");
File audioFile = new File(AudioFolder, "myaudio.mp4");
Uri audioFileUri = Uri.fromFile(audioFile);
audioIntent.putExtra(MediaStore.EXTRA_OUTPUT, audioFileUri);
startActivityForResult(audioIntent, 0);
Audio recording is working fine with me. After successful recording It has been stored default in sdcard. I am not able to store it with my particular folder "MyFolder" that is located in sdcard so It might be store in /sdcard/Myfolder/RecordedAudio/myaudio.mp4.
What is going wrong with my above code.?
Please help me with your suggestions.
Thanks

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();
}

Categories

Resources