Flutter :视频播放器未初始化

mgdq6dx1  于 2023-02-16  发布在  Flutter
关注(0)|答案(1)|浏览(257)

我有一个视频预览小部件,它接受字符串url或视频文件。如果参数是String,它从在线/该高速缓存下载文件。考虑到这一点,我的实现如下:

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:hero/helpers/cache_manager/cache_manager.dart';
import 'package:video_player/video_player.dart';
import 'package:chewie/chewie.dart';

class WaveVideoPreview extends StatefulWidget {
  final File? videoFile;
  final String? videoUrl;

  WaveVideoPreview({this.videoFile, this.videoUrl});

  @override
  _WaveVideoPreviewState createState() => _WaveVideoPreviewState();
}

class _WaveVideoPreviewState extends State<WaveVideoPreview> {
  late VideoPlayerController? _controller;
  late ChewieController _chewieController;

  void initState() {
    super.initState();

    _initAsync();
  }

  void _initAsync() async {
    File? _videoFile = widget.videoFile;
    if (_videoFile == null) {
      _videoFile = await getVideo(_videoFile);
    }

    _controller = VideoPlayerController.file(_videoFile!)
      ..initialize().then((_) {
        setState(() {
          _chewieController = ChewieController(
            videoPlayerController: _controller!,
            aspectRatio: _controller!.value.aspectRatio,
            autoPlay: false,
            looping: true,
            allowFullScreen: false,
          );
        });
      });
  }

  Future<File?> getVideo(File? _videoFile) async {
    _videoFile = await MyCache.getVideo(widget.videoUrl!);
    return _videoFile;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 200.0,
      child: (_controller?.value.isInitialized ?? false)
          ? Chewie(
              controller: _chewieController,
            )
          : SizedBox.shrink(),
    );
  }

  @override
  void dispose() {
    _controller!.dispose();
    _chewieController.dispose();
    super.dispose();
  }
}

static Future<File?> getVideo(String url) async {
    DefaultCacheManager _cacheManager = DefaultCacheManager();
    File? file = await _cacheManager.getSingleFile(url);
    return file;
  }

它将引发错误:

════════ Exception caught by widgets library ═══════════════════════════════════
The following LateError was thrown building WaveVideoPreview(dirty, state: _WaveVideoPreviewState#659ef):
LateInitializationError: Field '_controller@1875314998' has not been initialized.

The relevant error-causing widget was
WaveVideoPreview
lib/…/widget/wave_tile.dart:84
When the exception was thrown, this was the stack
#0      _WaveVideoPreviewState._controller (package:hero/screens/home/home_screens/views/waves/widget/video/wave_video_preview.dart)
package:hero/…/video/wave_video_preview.dart:1
#1      _WaveVideoPreviewState.build
package:hero/…/video/wave_video_preview.dart:57

有人知道这是怎么回事吗?我已经尝试改变视频播放器之间的可空和不可空,但仍然无济于事。也正如你所看到的,我有一个空检查,但仍然没有什么。

gajydyqb

gajydyqb1#

_initAsync正在使用异步,初始化控制器需要一些帧。
最好使用FutureBuilder来实现这一点,或者使这些函数可以为空,并在使用它时进行空检查。

VideoPlayerController? _controller;
   ChewieController? _chewieController;

相关问题