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();
      }
    });
  }
}
相关推荐
BG25 分钟前
Flutter Svg转Path对象,path.getBounds()获取测量信息不准问题记录
flutter
MaoJiu38 分钟前
Flutter中实现Hero Page Route效果
flutter
神经骚栋1 小时前
Flutter面试题01-Flutter中的三棵树
flutter·面试
小严家1 天前
Flutter完整开发指南 | Flutter&Dart – The Complete Guide
开发语言·flutter
倾云鹤1 天前
flutter实现Function Call
flutter·llm·function call
程序员老刘·2 天前
Flutter版本选择指南:避坑3.27 | 2025年9月
flutter·跨平台开发·客户端开发
懒得不想起名字2 天前
Flutter二维码的生成和扫描
flutter
鹏多多2 天前
flutter-详解控制组件显示的两种方式Offstage与Visibility
前端·flutter
猪哥帅过吴彦祖2 天前
Flutter 系列教程:常用基础组件 (上) - `Text`, `Image`, `Icon`, `Button`
android·flutter·ios