How to record audio file in Android - android

I am working in android. How can I record an audio file through microphone, and how can I save the recorded file in the emulator?

It is easy to record audio in Android.
What you need to do is:
1) Create the object for media record class : MediaRecorder recorder = new MediaRecorder();
2) In the emulator, you're unable to store the recorded data in memory, so you have to store it on the SD Card. So, first check for the SD Card availability: then start recording with the following code.
String status = Environment.getExternalStorageState();
if(status.equals("mounted")){
String path = your path;
}
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
3)
To stop, override stop method of activity
recorder.stop();
recorder.release();

Here is a good tutorial with sample code.
Audio Capture at Android Developer
it includes these steps:
Create a new instance of android.media.MediaRecorder.
Set the audio source using MediaRecorder.setAudioSource(). You will probably want to use MediaRecorder.AudioSource.MIC.
Set output file format using MediaRecorder.setOutputFormat().
Set output file name using MediaRecorder.setOutputFile().
Set the audio encoder using MediaRecorder.setAudioEncoder().
Call MediaRecorder.prepare() on the MediaRecorder instance.
To start audio capture, call MediaRecorder.start().
To stop audio capture, call MediaRecorder.stop().
When you are done with the MediaRecorder instance, call MediaRecorder.release() on it. Calling MediaRecorder.release() is always recommended to free the resource immediately.

Basically, there is no easy way to write audio file as simple WAV file. AudioRecorder produces data in raw Linear PCM format. And Android's API only gives you audio data buffers.
You'll need to create WAV file yourself. What this means for you, is that you need to add all the chunks yourself: RIFF header, FMT and DATA chunks.

I had tried to record the WAV file in android. But somehow, it is only recording the. WAV file in stereo only and you should use the audio recorder with the following parameter
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
RECORDER_SAMPLERATE, AudioFormat.CHANNEL_IN_MONO,RECORDER_AUDIO_ENCODING, bufferSize);
recorder.startRecording();
and from the recorder, you need to write all the data into one file and also you need to give header information
then just do
recorder.stopRecording();
but I have just one problem in this is even if I give AudioFormat.CHANNEL_IN_MONO..it still record in stereo format. Hope my answer helps you.

Related

Converting sound of recorded audio into byte array in Android

I am developing an Android music identification app like Shazam. I searched online and I found a lot of articles. But the best one I found is https://www.toptal.com/algorithms/shazam-it-music-processing-fingerprinting-and-recognition because it has simple code, explained as a tutorial and clearly explained.
But in android, I cannot follow the code exactly because some code does not has in android and some are different in syntax. As you can see in the link, it record the sound and finally convert that into byte array. But in android, what I am doing is I recorded the song using MideaRecorder and save it into a media file with .3gp or .mp3 extension. Now I am saving as .3gp. In the link I am following, it directly record the sound and convert the sound data(Frequency, Vibration and so on) into byte array.
What I am actually doing in android is trying to convert media file into byte array. So my question is, when I convert the audio file into byte array, will I get the sound data that need to be processed like in Shazam? Can I do that? The way I am doing is right? Can I do Fourier Transform on that byte array received from recording?
I am recording sound like this:
outputFile = Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording.3gp";
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
mediaRecorder.setOutputFile(outputFile);
mediaRecorder.prepare();
mediaRecorder.start();
I am trying to convert that file into byte array for FFT.

How to record audio m4a format audio file in android?

I am able to record audio file as .m4 format using media recorder,but it is not support in all media player.so i want to record audio file in m4a like iOS in android.
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("test.mp4");
So, Is there any way i can record audio file in m4a format in android?
Please reply if anyone have solution for this issue.
Thanks in advance..
i think you have to set the
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
i've a problem with playing the recorded sound on Windows. On my Android Smartphone i can load the recorded file und play them.
Could you jagdish play the recorded sound on Windows?
I have the same problem, my application writes some files and I try a web portal where it is possible to listen to these audios, for this to work I changed the enconder to AudioEncoder.AAC and it worked well for me, but the file size is much larger, if you have no problem with the file size, maybe it will work for you, too.

Android: Save Mediarecorder-Stream as playable file

I want to record videos with my android device (Nexus 10) and upload it later to youtube.
So far I'am recording with the android MediaRecoder and stream it via LocalSocket to save the data to multiple files. But the files are not playable.
I read some articles that sine API-Level 18 it is possible to convert files with MediaCodec and/or MediaMuxer. And i found this this code, but i do not really understand how to handle it.
Has anybody an easy example which shows how to convert the raw data from the LocalSocket to a playable file (i.e. mp4 files)?
My MediaRecoder looks like this:
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
camcorderProfile_HQ.fileFormat = MediaRecorder.OutputFormat.MPEG_4;
camcorderProfile_HQ.videoCodec = MediaRecorder.VideoEncoder.MPEG_4_SP;
recorder.setProfile(camcorderProfile_HQ);
recorder.setPreviewDisplay(surfaceHolder.getSurface());
clientSocket = new LocalSocket();
clientSocket.connect(new LocalSocketAddress(SOCKET_ADDRESS));
recorder.setOutputFile(clientSocket.getFileDescriptor());
recorder.prepare();
recorder.start();
Thanks in advance.
I don't think you can do it like that. Writing to a mp4 muxed file requires the file descriptor to be seekable. Unix sockets are not. This is probably because muxing to mp4 commonly requires the muxer to seek back and forth to write indices etc... and that cannot be done on local sockets.
If you can't simply output to file and then copy it, this might be very interesting for you: http://hello-qd.blogspot.it/2013/05/how-to-process-mediarecorder-frames.html.

Implementing media player in Android application

I am developing an application in which I have to perform three functions play, record and pause an audio file.
Has anyone implemented it before?
Take a look at MediaPlayer or AudioTrack for playback. The difference is that MediaPlayer can play several audio formats directly from file (or in some cases even from remote URL), while AudioTrack plays only from raw LPCM buffer,
For recording take a look at MediaRecorder or AudioRecord. The difference is that MediaRecorder records audio and video to .3gp, while AudioRecord gives you only audio as raw LPCM buffers. The AudioRecord data can be used to create .wav files (though some extra code is required for this).

Video recording format(.3gp or mp4) in android?

I use the Intent MediaStore.ACTION_VIDEO_CAPTURE video recording method.
By default, the recorded video is stored as a .3gp file. I want to record and store the video as a .mp4 file.
Is this possible?.
Yes
Set the MediaRecorder's OutputFormat to MPEG_4, like so:
recorder = new MediaRecorder();
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
This other SO answer describes it perfectly, as well.
Yes, you can have an mp4 output format, mp4 is just a container : see this to know what kind of data streams you can store in it.
MPEG_4 MPEG4 media file format
Also refer to this

Categories

Resources