Android: Save Mediarecorder-Stream as playable file - android

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.

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.

Video recording without specifing any output file

I'm aware of that there is a way of recording videos and saving it to a specific file.
Here's a code sample:
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile cpHigh = CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
recorder.setOutputFile("/sdcard/videocapture_example.mp4"); //Saving the file to the sd card
recorder.setMaxDuration(50000);
recorder.setMaxFileSize(5000000);
My question: Is it possible to record a video without specifing any output file, but the output would be a byte array which is built from the record? My target is to prevent saving any file to my SD card.
I don't really mind about using a 3rd party libraries, but prefer not to.
Thanks in advanced!
This idea has been used in Spydroid project here
Check the VideoStream class.
The output is set to a Local socket, then the data retrieved by another LocalSocket as a stream and may be manipulated as desired (writing to byte[]).

How to record audio file in 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.

Android:How To Get Media Recorder Output In MP3 Format?

I am creating an app that contains voice recording and playing. Is it possible to record files in MP3 format?
I need output file as mp3 format. Thanks.
There's currently no MP3 encoder built into the Android framework (as far as I know), so you can't do it out of the box. You need to add an MP3 encoding library to your project to do so.
For this, you can look at this stackoverflow post for a complete answer.
2015 update:
MP3 is officially supported by android - (edit) - however, this is only for decoding, not encoding.
http://developer.android.com/guide/appendix/media-formats.html
Core Media Formats: MP3 • Mono/Stereo 8-320Kbps constant (CBR) or
variable bit-rate (VBR) MP3 (.mp3)
(edit) - For smaller file sizes, you can use an .mp4. You can set it up with:
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
There is definitely an MP3 encoder on the Android framework.
Like I answered here on stackoverflow all you need to do is:
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
recorder.setOutputFile(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/myrecording.mp3");
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.prepare();
recorder.start();
The important part here is the setOuputFormat and the setAudioEncoder. MediaRecorder records playable mp3 if you're using MediaRecorder.OutputFormat.MPEG_4 and MediaRecorder.AudioEncoder.AAC together.
The funny thing is that this solution was a result of my experimenting with what works, and I have no idea as to why it works. I was hoping somebody could shed some light on this in the comments.
EDIT
As #JohnSmith and a few other pointed out in the comments, this does not encode the audio in MP3 format and there is no in-built MP3 encoder on Android.
The reason this solution seems to work is that MediaPlayer recognizes the encoding as AAC before beginning the track (regardless of the file extension). So although the output file's mime type is audio/mp4, the encoding is most definitely AAC.

Categories

Resources