从视频播放器的列表中选择时更改视频

时间:2020-05-12 11:26:47

标签: flutter video-player flutter-listview

我有一个页面具有此UI参数:

  1. 视频播放器
  2. 包含不同URL的视频列表

现在,当我从具有不同URL的列表中选择特定视频时,我想更改视频。

我正在使用两个软件包,分别是:video_playerchewie。现在的问题是,即使经过大量的努力,我仍然收到错误消息:

I/flutter (30845): Another exception was thrown: A VideoPlayerController was used after being disposed.

我已经在StackOverFlow上提到了以下答案,但是对于我来说似乎没有任何作用:

这是我的代码:

class VideoPlayerPageState extends State<VideoPlayerPage> {
  VideoPlayerController _playerController;
  ChewieController _chewieController;

  String _videourl = 'https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4';

  @override
  void initState() {
    // Initialising the player
    _initPlayer(_videourl);

    super.initState();
  }

  // Player intialisation for video player
  void _initPlayer(String url){
    // Initialising Controller
      _playerController = VideoPlayerController.network(url);
      _chewieController = ChewieController(
      videoPlayerController: _playerController,
      aspectRatio: 16/9,
      autoPlay: true,
      looping: false,
      autoInitialize: true,
      allowFullScreen: true,
      allowedScreenSleep: false,
      allowMuting: true
    );
  }

  @override
  void dispose() {
    // Disposing the controllers when not required
    _playerController.dispose();
    _chewieController.dispose();

    super.dispose();
  }

  // This is playlist card widget which controlls the playlists
  Widget videoPlayListCard(String videoURL){
      // Widget 
      return GestureDetector(
      onTap: (){
        // To change the video for the controller
        _onControllerChange(videoURL);
      },
      child: Container()
    );
  }

  void _onControllerChange(String link) async{
    if(_playerController == null){
      _initPlayer(link);
    }else {
      // If there was an old controller, we need to dispose it first
      final _oldPlayerController = _playerController;

      // Registering a callback for the end of next frame
      // to dispose of an old controller
      // (which won't be used anymore after calling setState)
      WidgetsBinding.instance.addPostFrameCallback((_) async { 
        await _oldPlayerController.dispose();

        // Initiating new controller
        _initPlayer(link);
      });

      // Making sure that controller is not used by setting it to null
      setState((){
        _playerController = null;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle(
        statusBarIconBrightness: Brightness.light 
      ),
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        theme: ThemeData.light().copyWith(),
        home: Scaffold(
          extendBodyBehindAppBar: true,
          body: ListView(
            shrinkWrap: true,
            children: [
              videoPlayListCard('https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4'),
              videoPlayListCard('https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4'),
           ]
         )
      )
    );
}}

在始终无法正常使用此功能时,我总是遇到错误。请帮助。谢谢

0 个答案:

没有答案