美文网首页
完美解决flutter video_player的bug

完美解决flutter video_player的bug

作者: 李新阳 | 来源:发表于2022-07-15 15:50 被阅读0次

    Video player had error com.google.android.exoplayer2.ExoPlaybackException: MediaCodecVideoRenderer error

    Video player had error com.google.android.exoplayer2.ExoPlaybackException: MediaCodecVideoRenderer error, index=0, format=Format(1, null, null, video/avc, null, -1, null, [1280, 720, 29.973475], [-1, -1]), format_supported=YES

    video_player 频繁init 和dispose就会报这个错,我看到好多老外也有遇到这个bug,当然也可以用其他视频库比如better_player,基本上这个报错的原因就是初始化的问题,关键代码在于这几句

    @override
    void initState() {
    super.initState();
    _controller = VideoPlayerController.network(url);
    _controller.initialize().onError((error, stackTrace) {
    isInitControl = _controller.value.isInitialized;
    print(
    "init error:{error.toString()}+{_controller.value.isInitialized}+");
    });
    }

    Future<bool> init() async {
    // _controller = VideoPlayerController.network(url);
    //
    // await _controller.initialize().timeout(Duration(seconds: 3),onTimeout: onTimeout).onError((error, stackTrace){
    // isInitControl=_controller.value.isInitialized;
    // print("init error:{error.toString()}+{_controller.value.isInitialized}+");
    // });
    await _controller.play().onError((error, stackTrace) {
    print("play error:{error.toString()}+{url}+");
    });
    return true;
    }

    这是布局的代码,
    FutureBuilder<bool>(
    future: init(),
    builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
    if (snapshot.data == true) {
    isInitControl = _controller.value.isInitialized;

                return AspectRatio(
                  aspectRatio: _controller.value.aspectRatio,
                  child: Stack(
                    alignment: Alignment.bottomCenter,
                    children: <Widget>[
                      VideoPlayer(
                        _controller,
                        key: UniqueKey(),
                      ),
                      ClosedCaption(text: _controller.value.caption.text),
                      _ControlsOverlay(controller: _controller),
                      VideoProgressIndicator(_controller, allowScrubbing: true),
                    ],
                  ),
                );
              } else {
                return const Text('视频加载中');
              }
            },
          ),
    

    没有用你给我一个大比斗

    相关文章

      网友评论

          本文标题:完美解决flutter video_player的bug

          本文链接:https://www.haomeiwen.com/subject/vqqcirtx.html