异常如下:
Picture Width(1080) or Height(2163) invalid, should N*2
报错的地方是MediaCodec.configure
mMediaCodec.configure(mMediaFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
引起这个崩溃的原因正如错误描述的那样,意思是:视频的宽和高必须是2的证书倍,及width=N*2,height=N*2
所以解决办法也非常简单,只需要重新设置视频的宽高,并做一下变形使其成为2的整数倍就OK了。
可以进行如下设置:
int formatWidth = width; int formatHeight = height; if ((formatWidth & 1) == 1) { formatWidth--; } if ((formatHeight & 1) == 1) { formatHeight--; }
使用的时候直接使用formatWidth和formatHeight,然后你就会发现以上的异常就会被修复。