2

So, I have encoded a h264 elementary stream with MediaCodec by collectings the frames using Camera's onPreviewFrame method.(using Encoding H.264 from camera with Android MediaCodec). Then, I generated an mp4 video using the h264 stream. Unfortunately, it doesn't have any audio in it.

I notice that MediaCodec should also allow encoding audio because it has settings for audio codecs.

Now is there any ways to add audio to the h264 stream?

thanks for reading and would appreciate any comments or suggestions.

Community
  • 1
  • 1
xiaowoo
  • 2,248
  • 7
  • 34
  • 45
  • hope this link help you : **[AudioVideoRecordingSample](https://github.com/saki4510t/AudioVideoRecordingSample)** – Ali Nov 20 '18 at 06:43

1 Answers1

2

A given instance of MediaCodec will encode either video or audio. You would need to create a second instance of MediaCodec to do the audio encoding, and then combine the streams with the MediaMuxer class (introduced in Android 4.3, API 18).

There are examples of using MediaMuxer on bigflake, but at the time I'm writing this there isn't one that demonstrates combining audio and video (they're just "muxing" the video into a .mp4 file). It should be enough to demonstrate how to use the class though.

fadden
  • 51,356
  • 5
  • 116
  • 166
  • hi fadden. first thanks for taking your precious time to answer my question. with your suggestion, I am now trying to encode video and audio seperately. However I am also encountering a new problem. That is I am not able to play the recorded audio file. I have created a new stackoverflow post for it(http://stackoverflow.com/questions/19826809/encoding-aac-audio-using-audiorecord-and-mediacodec-on-android) ... let me know if you have other suggestions. thanks – xiaowoo Nov 07 '13 at 02:24