Playing audio from amr on Android
Question
I can receive a byte array from a Socket
. I need to play audio from this byte array - the audio is encoded with AMR 8000Hz.
I found that I can play AMR audio with MediaPlayer
. However, MediaPlayer
can't play music from byte array, and I don't want to write them to file.
Is there a way to play AMR sound from byte array on android?
La solution
The Android framework allows you to play back audio data directly from memory using the AudioTrack
class; the drawback is that the audio must already be decoded into PCM data. If you are lucky enough to target Android 4.1, there are new APIs that allow you to decode the data separately so it can be passed to AudioTrack
(see MediaExtractor
and MediaCodec
). However, prior to that there were no exposed APIs for encoding/decoding beyond MediaRecorder
and MediaPlayer
.
If targeting a version of Android prior to 4.1 (which I imagine you probably are) you have two options:
- Find a 3rd party decoder for the AMR data so you can pass it on to
AudioTrack
- Save your data to a file (even temporarily) so it can be handed to
MediaPlayer
HTH