颤抖地布置视频并再次使用

时间:2019-02-11 05:53:42

标签: flutter android-videoview dispose

我想循环浏览媒体文件(图像,视频等)列表,所以我有一个Future,它可以使自己遍历该列表并显示每个媒体项目。 如果我的列表包含例如[视频,视频,图像,视频],但我希望使用以下方式,我希望它能够依次播放视频:

void playVideo(File video) {
  if(playerController != null && playerController.value.initialized) {
    playerController.removeListener(listener);
    playerController.dispose();
  }
  playerController = new VideoPlayerController.file(video);
  playerController.initialize().then((_) => setState(() {}));
  //playerController.setVolume(0.0);
  playerController.play();
  playerController.addListener(listener);
}

并在每次有新视频要显示时调用playVideo。 如果这样做,则会出现以下错误:

A VideoPlayerController was used after being disposed.
Once you have called dispose() on a VideoPlayerController, it can no longer be used.

1 个答案:

答案 0 :(得分:0)

在下面的“再次播放代码”按钮中,单击“更改和处置视频”

class MainScreen extends StatefulWidget {
  MainScreen({Key key}) : super(key: key);

  @override
  _MainScreenState createState() => new _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  List<String> urlsVideo = [
    "assets/videos/1.1.mp4",
    "assets/videos/1.2.mp4",
  ];

  int videoPos = 1;

  VideoPlayerController controllerFirst;

  StreamController<bool> streamController = new StreamController();

  @override
  void initState() {
    _startVideoPlayer();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      backgroundColor: Colors.white,
      body: Column(
        children: <Widget>[
          Expanded(
              child: StreamBuilder(
                  stream: streamController.stream,
                  builder: (context, snapshot) {
                    if (snapshot.hasData &&
                        !snapshot.data &&
                        controllerFirst != null) {
                      return VideoPlayer(controllerFirst);
                    } else {
                      return Center(child: CircularProgressIndicator());
                    }
                  })),
          RaisedButton(
            child: Padding(
              padding: const EdgeInsets.all(10.0),
              child: Text("Play Again"),
            ),
            onPressed: () {
              _startVideoPlayer();
            },
          )
        ],
      ),
    );
  }

  Future<void> _startVideoPlayer() async {
    videoPos = videoPos == 0 ? 1 : 0;
    streamController.add(true);
    final VideoPlayerController _controller =
        VideoPlayerController.asset(urlsVideo[videoPos]);
    _controller.addListener(_listener);
    await _controller.setLooping(true);
    await _controller.initialize();
    final VideoPlayerController oldController = controllerFirst;
    if (mounted) {
      setState(() {
        controllerFirst = _controller;
      });
    }
    await _controller.play();
    await oldController?.dispose();
    streamController.add(false);
  }

  get _listener => () {
        if (controllerFirst != null && controllerFirst.value.size != null) {
          if (mounted) setState(() {});
          controllerFirst.removeListener(_listener);
        }
      };
}