8

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.

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Varu
  • 311
  • 1
  • 15

1 Answers1

6

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.

mstorsjo
  • 12,983
  • 2
  • 39
  • 62
  • Regarding stability, Is MediaCodec class has equal stability compared to Media Recorder? Does MediaRecorder internally use MediaCodec? – Ayyappa Feb 05 '22 at 06:37