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();
      }
    });
  }
}
相关推荐
蒋星熠2 分钟前
Flutter跨平台工程实践与原理透视:从渲染引擎到高质产物
开发语言·python·算法·flutter·设计模式·性能优化·硬件工程
卢叁3 小时前
Flutter之自定义TabIndicator
前端·flutter
萧雾宇4 小时前
Android Compose打造仿现实逼真的烟花特效
android·flutter·kotlin
拜无忧5 小时前
【教程】flutter常用知识点总结-针对小白
android·flutter·android studio
拜无忧6 小时前
【教程】Flutter 高性能项目架构创建指南:从入门到高性能架构
android·flutter·android studio
醉过才知酒浓6 小时前
flutter 拦截返回按钮的方法(WillPopScope or PopScope)
flutter
傅里叶8 小时前
sudo启动Flutter程序AMD初始化失败
linux·flutter
苦逼的搬砖工9 小时前
Flutter UI Components:闲来无事,设计整理了这几年来使用的UI组件库
前端·flutter
黑金IT10 小时前
Dart → `.exe`:Flutter 桌面与纯命令行双轨编译完全指南
flutter
iOS_MingXing11 小时前
flutter TabBar 设置isScrollable 第一个有间距
flutter