What happened
最近负责的项目中碰到了在部分手机上无法播放视频的问题,我们接入的是 ExoPlayer 三方库,从 log 看出现的是 Decoder init failed,也是网上常见的 4001 (ERROR_CODE_DECODER_INIT_FAILED)的问题。
在 Google 搜索无果后,决定深入源码中去一步一步探究,找到问题的所在,果然功夫不负有心人,最终从源码中找到了解决方案,分享出来希望也能帮助到大家。
Google 上相关问题: https://github.com/google/ExoPlayer/issues/8987
Find the bug
应用对比
由于我之前负责其他的项目中也使用了 ExoPlayer 来播放的动态壁纸的,在这几个机型上测试发是可以正常播放动态壁纸,这样可以大概率排除是机型的问题。
随即引入 ExoPlayer 库写了一个简单的 Demo,测试对比发现在该机型上可以播放网上找一个视频链接,但无法播放我们的视频链接,初步怀疑视频格式在某些机型上不支持。
源码分析
从 log 中可以看出是 MediaCodecVideoRenderer 抛出了 ExoPlaybackException,从调用栈关系可以发现最终是调用到了 MediaCodecRenderer -> maybeInitCodecWithFallback() ,然后再去源码中分析其逻辑。
private void maybeInitCodecWithFallback(...) {
...
while (codec == null) {
...
try {
initCodec(codecInfo, crypto);
} catch (Exception e) {
Log.w(TAG, "Failed to initialize decoder: " + codecInfo, e);
DecoderInitializationException exception = new DecoderInitializationException(inpu