Skip to content

[video_player] Add a state event for video completion #21929

Closed
flutter/packages
#4923

Description

@once10301
No description provided.

Activity

  1. changed the title [-]inpluns/video_player How can I monitor the completion[/-] [+]plugins/video_player How can I listener the completion?[/+] on Sep 17, 2018
  2. yasincidem commented on Sep 17, 2018

    @yasincidem

    A solution would be something like the code below until video_player has a isEnded or isFinished method.

    import 'package:flutter/material.dart';
    import '../widgets/YourWidget.dart';
    import 'package:video_player/video_player.dart';
    import 'dart:async';
    
    class YourState extends State<YourWidget> {
      VideoPlayerController _controller;
      bool _isPlaying = false;
      Duration _duration;
      Duration _position;
      bool _isEnd = false;
    
      @override
      void initState() {
        super.initState();
        _controller = VideoPlayerController.network(
          'http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_20mb.mp4',
        )
        ..addListener(() {
          final bool isPlaying = _controller.value.isPlaying;
          if (isPlaying != _isPlaying) {
            setState(() {
              _isPlaying = isPlaying;
            });
          } 
          Timer.run(() {
              this.setState((){
                _position = _controller.value.position;
              });
            });
            setState(() {
              _duration = _controller.value.duration;
            });
            _duration?.compareTo(_position) == 0 || _duration?.compareTo(_position) == -1 ? this.setState((){
              _isEnd = true;
            }) : this.setState((){
              _isEnd = false;
            });
          })
        ..initialize();
      }
    
     @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: _position.toString(),
          home: Scaffold(
            body: Column(
              children: <Widget> [
                _controller.value.initialized
                  ? AspectRatio(
                      aspectRatio: _controller.value.aspectRatio,
                      child: VideoPlayer(_controller),
                    )
                  : Container(),
                Text('Duration ${_duration?.toString()}'),
                Text('Position ${_position?.toString()}'),
                Text('isEnd?  $_isEnd'),
              ]
            ),
            floatingActionButton:  FloatingActionButton(
              onPressed: _controller.value.isPlaying
                  ? _controller.pause
                  : _controller.play,
              child: Icon(
                _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
              ),
            ),
          ),
        );
      }
    }
    
  3. once10301 commented on Sep 18, 2018

    @once10301
    Author

    Thank you, I have another question.

    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    import 'package:video_player/video_player.dart';
    
    class SplashActivity extends StatefulWidget {
      @override
      SplashState createState() => new SplashState();
    }
    
    class SplashState extends State<SplashActivity> {
      VideoPlayerController controller;
      VoidCallback listener;
    
      @override
      void initState() {
        super.initState();
        controller = VideoPlayerController.asset('assets/analects.mp4');
        listener = () {
          if (controller.value.initialized) {
            Duration duration = controller.value.duration;
            Duration position = controller.value.position;
            if (duration.compareTo(position) != 1) {
              Navigator.pushReplacementNamed(context, '/main');
            }
          }
        };
        controller
          ..addListener(listener)
          ..setVolume(1.0)
          ..initialize();
        controller.play();
      }
    
      @override
      void deactivate() {
        super.deactivate();
        controller.removeListener(listener);
        controller.setVolume(0.0);
      }
    
      @override
      Widget build(BuildContext context) {
        /// 隐藏状态栏
        SystemChrome.setEnabledSystemUIOverlays([]);
        return Scaffold(
          body: Container(
            child: VideoPlayer(controller),
          ),
        );
      }
    }
    I/flutter (28816): ══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════
    I/flutter (28816): The following assertion was thrown while dispatching notifications for VideoPlayerController:
    I/flutter (28816): Navigator operation requested with a context that does not include a Navigator.
    I/flutter (28816): The context used to push or pop routes from the Navigator must be that of a widget that is a
    I/flutter (28816): descendant of a Navigator widget.
    
  4. once10301 commented on Sep 18, 2018

    @once10301
    Author

    I find another question.
    in iOS this can't work.

    I used

    print('duration:${controller.value.duration}');
    print('position:${controller.value.position}');
    flutter: duration:0:00:08.040000
    flutter: position:0:00:00.000000
    flutter: duration:0:00:08.040000
    flutter: position:0:00:00.000000
    flutter: duration:0:00:08.040000
    flutter: position:0:00:01.153000
    flutter: duration:0:00:08.040000
    flutter: position:0:00:01.650000
    flutter: duration:0:00:08.040000
    flutter: position:0:00:02.152000
    flutter: duration:0:00:08.040000
    flutter: position:0:00:02.652000
    flutter: duration:0:00:08.040000
    flutter: position:0:00:03.152000
    flutter: duration:0:00:08.040000
    flutter: position:0:00:03.656000
    flutter: duration:0:00:08.040000
    flutter: position:0:00:04.160000
    flutter: duration:0:00:08.040000
    flutter: position:0:00:04.650000
    flutter: duration:0:00:08.040000
    flutter: position:0:00:05.153000
    flutter: duration:0:00:08.040000
    flutter: position:0:00:05.652000
    flutter: duration:0:00:08.040000
    flutter: position:0:00:06.150000
    flutter: duration:0:00:08.040000
    flutter: position:0:00:06.656000
    flutter: duration:0:00:08.040000
    flutter: position:0:00:07.152000
    flutter: duration:0:00:08.040000
    flutter: position:0:00:07.656000
    flutter: duration:0:00:08.040000
    flutter: position:0:00:07.656000
    
  5. added this to the milestone on Sep 25, 2018
  6. jffiorillo commented on May 17, 2019

    @jffiorillo

    @once10301
    Have you tried using the following listener?

    listener = () {
      if (controller.value.initialized && !controller.value.isPlaying){
        Navigator.pushReplacementNamed(context, '/main');
      }
    };
    

    I used in iOs and it worked for me...

    Or you allow the user to stop the video manually?

  7. modified the milestones: , on Jan 7, 2020
  8. added
    P3Issues that are less important to the Flutter project
    on May 29, 2020
  9. removed this from the milestone on Aug 17, 2020
  10. jerryzhoujw commented on Aug 24, 2020

    @jerryzhoujw
    Contributor

    You can set the loop:false, then when it play finished, fire another play if you want loop feature. and do the thing you want in that finished event.

  11. 10 remaining items

  12. Syed-Waleed-Shah commented on Jun 11, 2023

    @Syed-Waleed-Shah

    Here is the simplest solution:

    videoPlayerController.addListener(() {
                    if (videoPlayerController.value.position ==
                        videoPlayerController.value.duration) {
                      // Video finished playing. Do whatever you want.
                    }
                  });
    
  13. changed the title [-]plugins/video_player How can I listener the completion?[/-] [+][video_player] Add a state event for video completion[/+] on Jul 9, 2023
  14. carrasc0 commented on Aug 7, 2023

    @carrasc0

    Not working, sometimes for some reason the position does not reach the end, in my case for exmaple the listener reah until here:

    Position: 0:00:10.908000
    duration: 0:00:11.436000

  15. github-actions commented on Oct 2, 2023

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  16. locked as resolved and limited conversation to collaborators on Oct 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectc: new featureNothing broken; request for a new capabilitycustomer: castawayp: video_playerThe Video Player pluginpackageflutter/packages repository. See also p: labels.team-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions