I'm a bit confused about how to play and record video/audio in Android. I don't really understand in what situations one should use these classes:
-To play: MediaPlayer
vs MediaExtractor
+ MediaCodec
-To record: MediaRecorder
vs MediaCodec
+ MediaMuxer
When do I have to use one or the others? Sorry if it's a repeated question, I think it should be a common one but I haven't found any.
MediaCodec class can be used to access low-level media codecs, i.e. encoder/decoder components. It is part of the Android low-level multimedia support infrastructure (normally used together with MediaExtractor , MediaSync , MediaMuxer , MediaCrypto , MediaDrm , Image , Surface , and AudioTrack .)
MediaPlayer class can be used to control playback of audio/video files and streams.
One of the most important components of the media framework is the MediaPlayer class. An object of this class can fetch, decode, and play both audio and video with minimal setup. It supports several different media sources such as: Local resources.
Recorded media output is in MP4 Audio+Video format, which is the same format that Teams uses to record media.
If the high level interfaces (MediaPlayer, MediaRecorder) can do what you want (play back video from a format that the system supports to the display, or record video from the camera into a file), you should probably just use them, it will be much much simpler.
If you want to do something more custom, when you notice that the part of the chain that you want to modify is hidden inside the high level classes, you'll want to move on to the lower level ones. E.g. for MediaExtractor; if you only want to extract packets of data from a file but not decode and display/play them back them immediately, you'll want to use MediaExtractor. If you want to receive packets from some other source that the system itself doesn't support, you'll want to use MediaCodec without MediaExtractor. Likewise, if you want to record something else than the camera, or write the output somewhere else than to a file that MediaRecorder supports, you'll want to use MediaCodec directly instead of MediaRecorder.
Also note that the high level classes also improve and get more flexible with newer API versions, allowing you to do things that previously required you to manually use the lower level classes. E.g. in Android 5.0, MediaRecorder got the ability to record from a custom Surface, allowing you to record a video of something you render yourself, not just the camera. This was previously possible since 4.3 by using the lower level classes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With