E/ExoPlayerImplInternal(19214): Source error.
E/ExoPlayerImplInternal(19214): com.google.android.exoplayer2.upstream.HttpDataSource$InvalidResponseCodeException: Response code: 500
E/ExoPlayerImplInternal(19214): at com.google.android.exoplayer2.upstream.DefaultHttpDataSource.open(DefaultHttpDataSource.java:300)
E/ExoPlayerImplInternal(19214): at com.google.android.exoplayer2.upstream.DefaultDataSource.open(DefaultDataSource.java:250)
E/ExoPlayerImplInternal(19214): at com.google.android.exoplayer2.upstream.TeeDataSource.open(TeeDataSource.java:53)
E/ExoPlayerImplInternal(19214): at com.google.android.exoplayer2.upstream.cache.CacheDataSource.openNextSource(CacheDataSource.java:479)
E/ExoPlayerImplInternal(19214): at com.google.android.exoplayer2.upstream.cache.CacheDataSource.open(CacheDataSource.java:308)
E/ExoPlayerImplInternal(19214): at com.google.android.exoplayer2.upstream.StatsDataSource.open(StatsDataSource.java:83)
E/ExoPlayerImplInternal(19214): at com.google.android.exoplayer2.source.ExtractorMediaPeriod$ExtractingLoadable.load(ExtractorMediaPeriod.java:885)
E/ExoPlayerImplInternal(19214): at com.google.android.exoplayer2.upstream.Loader$LoadTask.run(Loader.java:381)
E/ExoPlayerImplInternal(19214): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
E/ExoPlayerImplInternal(19214): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
E/ExoPlayerImplInternal(19214): at java.lang.Thread.run(Thread.java:761)
调试半天发现是initialize()函数失败报的错误。由于这个库底层是使用了另一个视频库,要调试那个库太麻烦了。想到了一个简单的处理方案,发现initialize函数报错的时候,VideoPlayerController.addListener监听会有一次触发。所以,就在被触发的时候,判断VideoPlayerController.value.initialized,是否初始化成功,如果成功就正常调用播放事件,如果失败重新初始化。经测试出现错误的时候会失败2次然后正常,可以设置一值来操作初始化的次数。
https://mp.csdn.net/editor/html/111617303
由于是取巧方案,有更好方案的同学可以交流下!!! 下面贴出关键代码
int frequency = 0;
@override
void initState() {
debugPrint("URL : ${widget._url}");
debugPrint("isNet: ${widget._isNet}");
frequency = 0;
if (widget._isNet) {
_controller = CachedVideoPlayerController.network(widget._url);
} else {
_controller = CachedVideoPlayerController.file(File(widget._url));
}
initialize();
// 播放状态
_controller.addListener(() {
if (!_controller.value.initialized) {
if (frequency < 20) {// 设置20次初始化,免得无限次导致OOM
initialize();
} else {
ToastUtil.show("视频文件初始化错误,请检查文件是否错误");
}
} else {
final bool isPlaying = _controller.value.isPlaying;
if (isPlaying != _isPlaying) {
setState(() {
_isPlaying = isPlaying;
});
}
}
});
super.initState();
}
Future initialize() async {
frequency++;
await _controller.initialize();
setState(() {
_controller.play();
});
}