ExoPlayer适配问题:Android 9及以上版本无法播放网络电台流
问题解决:Android 9+ ExoPlayer无法播放HTTP流
核心问题定位
你的网络安全配置文件中,<domain>标签格式错误——不能包含http://前缀,仅需填写纯域名。Android 9+对网络安全配置的校验更严格,这个错误会导致明文流量权限不生效,进而引发播放失败。
修复步骤
1. 修正网络安全配置文件
将res/xml/network_security_config.xml中的域名部分改为纯域名:
<?xml version="1.0" encoding="utf-8"?> <network-security-config> <base-config> <trust-anchors> <certificates src="system"/> </trust-anchors> </base-config> <domain-config cleartextTrafficPermitted="true"> <!-- 去掉http://前缀,仅保留纯域名 --> <domain includeSubdomains="true">stream.zeno.fm</domain> </domain-config> </network-security-config>
2. 确认Manifest配置有效性
确保AndroidManifest.xml中application标签的配置正确,无需额外修改:
<application ... android:usesCleartextTraffic="true" android:networkSecurityConfig="@xml/network_security_config"> ... </application>
3. 捕获ExoPlayer错误(可选但推荐)
添加错误监听,精准定位后续可能出现的问题:
player.addListener(new Player.Listener() { @Override public void onPlayerError(PlaybackException error) { // 打印错误信息,便于排查 Log.e("ExoPlayerError", error.getMessage(), error); } });
4. 验证流格式兼容性
确认ExoPlayer依赖包含对应流格式的支持库(常见的MP3/AAC流,核心库已支持),比如在build.gradle中确保依赖:
implementation "com.google.android.exoplayer:exoplayer-core:2.X.X" // 如需支持更多格式,可添加对应扩展库: // implementation "com.google.android.exoplayer:exoplayer-hls:2.X.X" // implementation "com.google.android.exoplayer:exoplayer-mp3:2.X.X"
额外说明
Android 9+默认禁用明文HTTP流量,即使配置了usesCleartextTraffic="true",如果network-security-config中的域名格式错误,系统仍会拦截明文请求,这就是Android 8及以下正常、9+失败的核心原因。
内容的提问来源于stack exchange,提问作者Moh'd Khamis Songoro
相关产品推荐
相关产品推荐

