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();
      }
    });
  }
}
相关推荐
忆江南9 小时前
iOS 深度解析
flutter·ios
明君879979 小时前
Flutter 实现 AI 聊天页面 —— 记一次 Markdown 数学公式显示的踩坑之旅
前端·flutter
恋猫de小郭10 小时前
移动端开发稳了?AI 目前还无法取代客户端开发,小红书的论文告诉你数据
前端·flutter·ai编程
MakeZero13 小时前
Flutter那些事-交互式组件
flutter
shankss13 小时前
pull_to_refresh_simple
flutter
shankss13 小时前
Flutter 下拉刷新库新特性:智能预加载 (enableSmartPreload) 详解
flutter
SoaringHeart2 天前
Flutter调试组件:打印任意组件尺寸位置信息 NRenderBox
前端·flutter
九狼2 天前
Flutter URL Scheme 跨平台跳转
人工智能·flutter·github
_squirrel2 天前
记录一次 Flutter 升级遇到的问题
flutter
Haha_bj3 天前
Flutter——状态管理 Provider 详解
flutter·app