I am recording audio on Android. Here is a relevant snippet
mediaRecorder = new MediaRecorder();
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
However, the resulting adts file is recorded with AAC LTP (Long Term Prediction) object type. Is there any way to force MediaRecorder to produce AAC LC on all devices which support it?
There is a bug in the media framework that incorrectly tags ADTS files as having the LTP object type, when it's actually LC. Such files then get rejected on playback because Android doesn't support LTP.
If you record using the THREE_GPP fileformat instead, the recording will be fine.
Related
I use code below generated a aac audio file. And the file plays fine on my windows machine. But it cannot be played on my android device using a MediaPlayer. What should I do to make the file playable on a android device? Thanks!
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
recorder.setAudioEncodingBitRate(16 * 44100);
recorder.setAudioSamplingRate(44100);
recorder.setAudioChannels(2);
recorder.setOutputFile(mTempFile.getAbsolutePath());
"Can Android play a ADTS AAC file?..."
Not with default apps. Only if you code such an app. Use the MediaCodec API to manually send each of your aac frame's bytes to the decoder.
If you can handle bytes and familiar with aac frame structre, then check these as starting points :
Decoding AAC using MediaCodec API on Android
PCM -> AAC (Encoder) -> PCM(Decoder) in real-time with correct optimization
"But it cannot be played on my android device using a MediaPlayer... What should I do to make the file playable on a android device?..."
The aac data must exist inside a container format like m4a or mp4. MediaPlayer (strangely) does not play raw aac data, yet does not expect raw mp3 to be contained.
note:
When placed inside one of the above, you will lose the ADTS header for each frame (since that information will now exist in other parts of the container's metadata).
I'm using the Android MediaRecorder to record AAC encoded audio files. Setting the output format to MPEG-4 worked pretty well. But as my audio player supports neither MPEG-4 nor 3GP I tried to get raw AAC files by using the output format AAC_ADTS, which is supported by Android since API level 16.
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
Here is where I got stuck. The MediaRecorder created a file but I'm not able to play that file with any player (neither Android's MediaPlayer nor the Windows Media Player nor my audio player I mentioned above, which was able to play an ADTS AAC file I found on the web).
Am I doing something wrong? Is the AAC_ADTS output format even a recommendable format? Is there a way to get an ADIF AAC file?
Use UNPROCESSED instead of MIC.
So, I have encoded a h264 elementary stream with MediaCodec by collectings the frames using Camera's onPreviewFrame method.(using Encoding H.264 from camera with Android MediaCodec). Then, I generated an mp4 video using the h264 stream. Unfortunately, it doesn't have any audio in it.
I notice that MediaCodec should also allow encoding audio because it has settings for audio codecs.
Now is there any ways to add audio to the h264 stream?
thanks for reading and would appreciate any comments or suggestions.
A given instance of MediaCodec will encode either video or audio. You would need to create a second instance of MediaCodec to do the audio encoding, and then combine the streams with the MediaMuxer class (introduced in Android 4.3, API 18).
There are examples of using MediaMuxer on bigflake, but at the time I'm writing this there isn't one that demonstrates combining audio and video (they're just "muxing" the video into a .mp4 file). It should be enough to demonstrate how to use the class though.
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.
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.