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();
      }
    });
  }
}
相关推荐
松☆1 小时前
Flutter + OpenHarmony 实战:构建离线优先的跨设备笔记应用
笔记·flutter
_大学牲3 小时前
Flutter 勇闯2D像素游戏之路(一):一个 Hero 的诞生
flutter·游戏·游戏开发
kirk_wang4 小时前
Flutter插件在鸿蒙端的开发与部署:跨生态桥梁的架构与实现
flutter·移动开发·跨平台·arkts·鸿蒙
勇气要爆发6 小时前
【第五阶段—高级特性和框架】复杂动画案例分析初体验
flutter
勤劳打代码8 小时前
追本溯源 —— SetState 刷新做了什么
flutter·面试·性能优化
松☆9 小时前
OpenHarmony 后台任务与 Flutter 生命周期协调:构建稳定可靠的混合应用
flutter
松☆9 小时前
Flutter 与 OpenHarmony 深度集成:自定义 MethodChannel 插件开发全指南
flutter·wpf
克喵的水银蛇10 小时前
Flutter 布局实战:掌握 Row/Column/Flex 弹性布局
前端·javascript·flutter
sunly_10 小时前
Flutter:实现多图上传选择的UI
flutter·ui