ExoPlayer自定义URI无法播放问题求助及代码排查
Hey there! I spot the exact issue here—you're using HlsMediaSource to load an MP4 video file, but that class is only meant for HLS streaming formats (like the .m3u8 URI that works perfectly). MP4 files are progressive media, so you need a different media source type to get it working.
Here's how to fix it step by step:
1. Replace HlsMediaSource with ExtractorMediaSource
Instead of using the HLS-specific source, use ExtractorMediaSource which handles standard container formats like MP4. Update your media source initialization code like this:
// 这是我想要播放的视频URI! Uri mp4VideoUri =Uri.parse("http://d17h27t6h515a5.cloudfront.net/topher/2017/April/58ffdcf9_9-final-product-brownies/9-final-product-brownies.mp4"); DefaultBandwidthMeter bandwidthMeterA = new DefaultBandwidthMeter(); DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "exoplayer2example"), bandwidthMeterA); ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory(); // 替换HlsMediaSource为ExtractorMediaSource,适配MP4格式 MediaSource videoSource = new ExtractorMediaSource(mp4VideoUri, dataSourceFactory, extractorsFactory, null, null); final LoopingMediaSource loopingSource = new LoopingMediaSource(videoSource); player.prepare(loopingSource);
2. Double-Check Key Prerequisites
Make sure these are in place to rule out other potential issues:
- INTERNET Permission: Verify you have
<uses-permission android:name="android.permission.INTERNET" />in yourAndroidManifest.xml—this is mandatory for loading remote video files. - URI Accessibility: Test the MP4 URI directly in a browser to confirm it’s publicly accessible and loads without errors.
- ExoPlayer Compatibility: Your code uses ExoPlayer 2.x, which fully supports MP4, but ensure your dependency versions are up to date to avoid format support gaps.
3. Enhance Error Logging (Optional)
To debug future issues more clearly, expand the onPlayerError method to log the full error stack trace:
@Override public void onPlayerError(ExoPlaybackException error) { Log.e(TAG, "Listener-onPlayerError...", error); // 打印完整错误信息 player.stop(); player.prepare(loopingSource); player.setPlayWhenReady(true); }
This will help you pinpoint exactly what’s going wrong if you run into similar playback issues later.
Once you make these changes, your MP4 video should play as expected!
内容的提问来源于stack exchange,提问作者Igor Grecco Lacourt

