flutter flutter_vlc_player播放视频设置循环播放失效、初始化后获取不到视频宽高

插件:flutter_vlc_player: ^7.4.3

问题1:设置循环播放_controller.setLooping(true);无用。

解决方法:

dart 复制代码
//vlcPlayer设置循环播放失效,以这种方式失效循环播放
_setLoopListener() async {
  if (_videoController!.value.hasError) {
    esLoadingToast('视频加载失败');
  } else {
    if (_videoController!.value.playingState == PlayingState.ended) {

      _videoController!.stop().then((_) => _videoController!.play());

    }
  }
}

initState(){
super.initState();
_videoController!.addListener(_setLoopListener);
}

问题2:播放器初始化完成但是元数据还未解析完成,导致无法获取到视频的宽高

dart 复制代码
_videoController?.addOnInitListener((){
 if (_videoController!.value.isInitialized) {//已经初始化
 final size = _videoController!.value.size;
 //打印出来获取到0
 print('width= ${size.with}');
 //打印出来获取到1
 print('aspectRatio= ${_videoController!.value.aspectRatio}');
 }
});

解决方法:

dart 复制代码
_videoController?.addOnInitListener((){
  _waitForVideoSize();
});

//视频虽然初始化完成但是元数据还未解析完成,使用轮询直到获取到宽高
_waitForVideoSize() {
  if (_videoController!.value.isInitialized) {
    int _attempt = 0;
    Timer.periodic(Duration(milliseconds: 300), (timer) {
      final size = _videoController!.value.size;
      if (size.width > 0 && size.height > 0) {
        timer.cancel(); // 停止轮询
        _totalDuration = _videoController!.value.duration;
        _videoController!.setVolume(0);
        _videoController!.setLooping(true);//VlcPlayer对于网络视频有时生效,有时失败
        double _videoWidth = _videoController!.value.size.width;
        double _videoHeight = _videoController!.value.size.height;
        print('_videoWidth= $_videoWidth  _videoHeight= $_videoHeight');
        print('aspectRatio= ${_videoController!.value.aspectRatio}');
        _sizeWidth = _videoWidth;
        _sizeHeight = _videoHeight;

        double _aspectRatio = _videoHeight / _videoWidth;
        _logic.videoAspectRatio = _aspectRatio;
        print('videoAspectRatio===== ${_logic.videoAspectRatio}');
        _videoController!.play();
        _isVideoInit = true;
        setState(() {});

      }else if(_attempt>= 200){
        timer.cancel();
      }
    });
  }
}
相关推荐
fouryears_2341740 分钟前
Flutter InheritedWidget 详解:从生命周期到数据流动的完整解析
开发语言·flutter·客户端·dart
LinXunFeng6 小时前
Flutter - 详情页 TabBar 与模块联动?秒了!
前端·flutter·开源
阅文作家助手开发团队_山神9 小时前
第三章: 解决Android iPad蓝牙键盘联想词UI不跟随光标问题
flutter
阅文作家助手开发团队_山神9 小时前
第四章:Flutter自定义Engine本地依赖与打包流程
前端·flutter
程序员老刘10 小时前
Flutter 3.35 更新要点解析
flutter·ai编程·客户端
阅文作家助手开发团队_山神12 小时前
第一章: Mac Flutter Engine开发准备工作
前端·flutter
EmmaGuo201514 小时前
flutter3.7.12版本设置TextField的contextMenuBuilder的文字颜色
前端·flutter
鹏多多.15 小时前
flutter-使用device_info_plus获取手机设备信息完整指南
android·前端·flutter·ios·数据分析·前端框架
来来走走20 小时前
Flutter开发 网络请求
android·flutter
SoaringHeart2 天前
Flutter进阶:高内存任务的动态并发执行完美实现
前端·flutter