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).
Related
I'm trying to mux a mp4 file using mediaMuxer. I've already set SPS, PPS and replaced the 4-bit header. When the h.264 frame formed with single slice, everything is ok, but when I change it into multi-slice, the result is wrong.
sounds multi-slice is not fully supported in android mediaMuxer, it will cause compatible issue when plays on iPhone/QuickTime player.
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.
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.
Is there anything that does the opposite of a MediaExtractor on android?
Take one or multiple streams from MediaCodecs (e.g. 1 video and 1 audio)
and package them in a container format for streaming or writing to files?
Looks like the answer is no.
Mostly because of the underlying API is designed for video streaming and not for video compression.
Writing encoder's output to file you'll get raw h264 file. Which can be played using mplayer or ffplay for example.
Also ffmpeg can be used to mux this raw file into some container. But first of all you need to build ffmpeg for android.
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.