HeadlinesBriefing favicon HeadlinesBriefing.com

Fixing Manual YUV Video Encoding Failures on Android

DEV Community •
×

Building a reliable video recorder on Android quickly turns into a nightmare. An app that runs flawlessly on a Pixel can spew green‑purple bands or crash on MediaTek and Exynos devices. The culprit lies in hidden differences in MediaCodec handling and YUV buffer layouts across OEMs. Developers often copy a bitmap to a byte array, assume stride equals width, and feed the result to the encoder.

On MediaTek chips that pad rows by 16–64 bytes, or on hardware that expects planar versus semi‑planar chroma ordering, the stream corrupts or the encoder throws IllegalArgumentException. Likewise, naïve surface locking races with the encoder thread, freezing the UI. The industry‑tested fix abandons manual conversion entirely.

By creating a COLOR_FormatSurface and letting the hardware perform stride, color conversion and alignment, apps achieve consistent output on Qualcomm, MediaTek and Exynos platforms from Android 10 through 15. Recommended patterns include dropping frames when the encoder is busy, using a ~100 ms timeout on dequeueOutputBuffer, cleaning up in the order MediaMuxer → MediaCodec → Surface, and confining all codec calls to a dedicated thread. Adding a foreground service on Android 12+ prevents background kills.

Testing on a range of chipsets, especially MediaTek‑powered phones, remains essential.