[Flutter]解决Cleartext HTTP traffic not permitted的问题
今天在播放网络媒体资源的开发中遇到了下面这个问题:
E/ExoPlayerImplInternal(20282): Caused by: com.google.android.exoplayer2.upstream.HttpDataSource$CleartextNotPermittedException: Cleartext HTTP traffic not permitted. See https://exoplayer.dev/issues/cleartext-not-permitted
E/ExoPlayerImplInternal(20282): at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.open(DefaultHttpDataSource.java:382)
E/ExoPlayerImplInternal(20282): at com.google.android.exoplayer2.upstream.DefaultDataSource.open(DefaultDataSource.java:263)
E/ExoPlayerImplInternal(20282): at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:84)
E/ExoPlayerImplInternal(20282): at com.google.android.exoplayer2.source.ProgressiveMediaPeriod$ExtractingLoadable.load(ProgressiveMediaPeriod.java:1005)
E/ExoPlayerImplInternal(20282): at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:412)
E/ExoPlayerImplInternal(20282): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
E/ExoPlayerImplInternal(20282): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
E/ExoPlayerImplInternal(20282): at java.lang.Thread.run(Thread.java:1012)
E/ExoPlayerImplInternal(20282): Caused by: java.io.IOException: Cleartext HTTP traffic to clips.vorwaerts-gmbh.de not permitted
E/ExoPlayerImplInternal(20282): at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:127)
E/ExoPlayerImplInternal(20282): at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:462)
E/ExoPlayerImplInternal(20282): at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:131)
E/ExoPlayerImplInternal(20282): at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.makeConnection(DefaultHttpDataSource.java:653)
E/ExoPlayerImplInternal(20282): at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.makeConnection(DefaultHttpDataSource.java:550)
E/ExoPlayerImplInternal(20282): at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.open(DefaultHttpDataSource.java:376)
E/ExoPlayerImplInternal(20282): ... 7 more
E/SurfaceSyncer(20282): Failed to find sync for id=0
W/Parcel (20282): Expecting binder but got null!
E/flutter (20282): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(VideoError, Video player had error com.google.android.exoplayer2.ExoPlaybackException: Source error, null, null)
E/flutter (20282):
上面异常提示:Cleartext HTTP traffic not permitted. See https://exoplayer.dev/issues/cleartext-not-permitted
查了一下,是Google的限制:
Google表示,为保证用户数据和设备的安全,针对下一代 Android 系统(Android P) 的应用程序,将要求默认使用加密连接,这意味着 Android P 将禁止 App 使用所有未加密的连接,因此运行 Android P 系统的安卓设备无论是接收或者发送流量,未来都不能明码传输,需要使用下一代(Transport Layer Security)传输层安全协议,而 Android Nougat 和 Oreo 则不受影响。
既然知道了问题所在,也就知道了解决问题的办法:
- 方法1、采用HTTPS协议,这个就直接避免了未加密的请求
- 方法2、将targetSdkVersion降低到27以下
方法3、增加网络安全配置的xml
在res下新增一个xml目录,创建一个名为network_security_config.xml
的文件(当然,名字可以自定义)<?xml version="1.0" encoding="utf-8"?> <network-security-config> <base-config cleartextTrafficPermitted="true" /> </network-security-config>
在Flutter项目中的
android/app/src/main/AndroidManifest.xml
文件下的application
标签增加以下属性:<?xml version="1.0" encoding="utf-8"?> <manifest ...> <uses-permission android:name="android.permission.INTERNET" /> <application ... android:networkSecurityConfig="@xml/network_security_config" ...> ... </application> </manifest>
方法4、在Flutter项目中的
android/app/src/main/AndroidManifest.xml
文件下的application
标签下增加android:usesCleartextTraffic
属性:<?xml version="1.0" encoding="utf-8"?> <manifest ...> <uses-permission android:name="android.permission.INTERNET" /> <application ... android:usesCleartextTraffic="true" ...> ... </application> </manifest>
我在这里解决这个问题是采用的第4种方案。
最后,在顺便给大家推荐几个可以正常用于测试的MP4视频源:
1、http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4
2、http://vjs.zencdn.net/v/oceans.mp4
3、https://media.w3.org/2010/05/sintel/trailer.mp4
4、https://media.w3.org/2010/05/sintel/trailer.mp4
其他各种格式,MP4, flv, mkv, 3gp 视频下载
https://www.sample-videos.com/index.php#sample-mp4-video