Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to record video and audio with MediaCodec and MediaMuxer

I am able to record(encode) video with the help of MediaCodec and MediaMuxer. Next, I need to work on audio part and mux audio with video with help of MediaCodec and MediaMuxer.

I am facing two problems:

  1. How to encode audio with MediaCodec. Do I need to encode audio and video in separate threads?

  2. How can I pass audio and video data to MediaMuxer (as writeSampleData() method takes only one type of data at a time)?

I referred to MediaMuxerTest but it is using MediaExtractor. I need to use MediaCodec as video encoding is done with MediaCodec. Please correct me if I am wrong.

Any suggestion or advice will be very helpful as there is no proper documentation available for these new APIs.

Note:

  1. My app is targeting to API 18+ (Android 4.3+).
  2. I have referred Grafika for video encoding.
like image 224
abhishek kumar gupta Avatar asked Oct 29 '14 11:10

abhishek kumar gupta


1 Answers

  1. No, you don't necessarily need a separate thread for audio, just use two separate MediaCodec instances.

  2. The first parameter of writeSampleData is trackIndex, which allows you to specify which track each packet corresponds to. (By running addTrack twice, once for each track, you get two separate track IDs.)

like image 76
mstorsjo Avatar answered Oct 07 '22 06:10

mstorsjo