问题
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:
How to encode audio with MediaCodec. Do I need to encode audio and video in separate threads?
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:
- My app is targeting to API 18+ (Android 4.3+).
- I have referred Grafika for video encoding.
回答1:
No, you don't necessarily need a separate thread for audio, just use two separate MediaCodec instances.
The first parameter of
writeSampleData
istrackIndex
, which allows you to specify which track each packet corresponds to. (By runningaddTrack
twice, once for each track, you get two separate track IDs.)
来源:https://stackoverflow.com/questions/26628677/how-to-record-video-and-audio-with-mediacodec-and-mediamuxer