ExoPlayer播放网络视频出现Source error问题求助
按照ExoPlayer开发者指南实现后,出现以下错误:
05-23 16:25:22.002 11893-11942/com.app.videotest E/ExoPlayerImplInternal: Source error.
实现代码:
// 1. Create a default TrackSelector BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter(); TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter); TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory); // 2. Create the player SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(this, trackSelector); // Bind the player to the view. PlayerView playerView = findViewById(R.id.player_view); playerView.setPlayer(player); // Produces DataSource instances through which media data is loaded. DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "yourApplicationName"), null); // This is the MediaSource representing the media to be played. MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory) .createMediaSource(Uri.parse("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")); // Prepare the player with the source. player.prepare(videoSource);
布局文件:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.google.android.exoplayer2.ui.PlayerView android:id="@+id/player_view" android:layout_width="200dp" android:layout_height="200dp" /> </android.support.constraint.ConstraintLayout>
完整日志片段:
05-23 16:25:18.919 11893-11893/? D/AppTracker: App Event: start 05-23 16:25:18.931 11893-11943/? D/OpenGLRenderer: HWUI GL Pipeline 05-23 16:25:18.946 11893-11945/? I/DpmTcmClient: RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor 05-23 16:25:18.951 11893-11945/? D/NetworkSecurityConfig: No Network Security Config specified, using platform default ... 05-23 16:25:22.002 11893-11942/com.app.videotest E/ExoPlayerImplInternal: Source error.
确认视频URL可正常访问,且已尝试多个不同URL,但仅能播放本地视频,无法播放网络视频,请问我遗漏了什么配置?
更新说明:播放本地视频时播放器可正常工作,但无法解决当前网络视频播放的问题。
你遇到的问题几乎可以肯定是网络权限缺失或者Android 9+的明文HTTP访问限制导致的——毕竟本地视频不需要网络,而网络视频必须依赖这两个关键配置。下面一步步来解决:
1. 检查并添加网络权限
首先确保你的AndroidManifest.xml中添加了必要的网络权限,没有这个权限APP根本无法发起网络请求:
<uses-permission android:name="android.permission.INTERNET" /> <!-- 可选但建议添加:帮助ExoPlayer判断网络状态,优化播放策略 --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
把这些权限标签放在manifest标签下、application标签之外的位置。
2. 解决Android 9+的明文HTTP限制
从Android 9(API级别28)开始,系统默认禁止APP使用明文HTTP请求,而你用的视频URL是http://开头的明文地址,这会被系统直接拦截。有两种解决方式:
方法一:直接允许明文流量(适合测试或全局需求)
在AndroidManifest.xml的application标签里添加android:usesCleartextTraffic="true":
<application ... android:usesCleartextTraffic="true" ...> ... </application>
方法二:配置网络安全规则(更安全,推荐生产环境)
创建res/xml/network_security_config.xml文件,可选择允许特定域名或全局的明文访问:
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <!-- 允许所有域名的明文访问 --> <base-config cleartextTrafficPermitted="true" /> <!-- 或者只允许指定域名的明文访问(更安全) --> <!-- <domain-config cleartextTrafficPermitted="true"> <domain includeSubdomains="true">vorwaerts-gmbh.de</domain> </domain-config> --> </network-security-config>
然后在AndroidManifest.xml的application标签中引用这个配置:
<application ... android:networkSecurityConfig="@xml/network_security_config" ...> ... </application>
3. 额外检查:确认ExoPlayer依赖完整
虽然你本地能正常播放,但还是建议确认Module级别的build.gradle中引入了完整的ExoPlayer依赖:
implementation "com.google.android.exoplayer:exoplayer-core:2.X.X" implementation "com.google.android.exoplayer:exoplayer-ui:2.X.X"
替换2.X.X为你使用的最新版本号即可。
做完以上配置后,重新编译运行APP,应该就能正常播放网络视频了。
内容的提问来源于stack exchange,提问作者Barazu

