0

I have a flutter camera app and am able to get a recorded video to play. The problem is, I am only able to get it to play when a button is pressed. How do I get the code to run when the widget(screen) is created instead of when the button is pressed so I don't have to press a button to get it to play? Here is my code:

Here is the code for when the button is pressed:

//raised button
    RaisedButton(
          onPressed: () {stopButtonPressed();},

//stopButtonPressed
void stopButtonPressed() {
          print('stopButtonPressed hit');
          stopVideoRecording().then((_) {
            print('StopVideoRecording complete');
          });
        }


//stopVideoRecording
Future<void> stopVideoRecording() async {
          print('stopVideoRecording hit');
          await _startVideoPlayer();
        }


//_startVideoPlayer

Future<void> _startVideoPlayer() async {
          print('_startVideoPlayer hit');
          print(Provider.of<SendDataModel>(context, listen: false).displayImageVideo());
          final VideoPlayerController vcontroller =
            VideoPlayerController.file(File(Provider.of<SendDataModel>(context, listen: false).displayImageVideo()));
          videoPlayerListener = () {
            if (videoController != null && videoController.value.size != null) {
              if (mounted) setState(() {});
              videoController.removeListener(videoPlayerListener);
            }
          };
          vcontroller.addListener(videoPlayerListener);
          await vcontroller.setLooping(true);
          await vcontroller.initialize();
          await videoController?.dispose();
          if (mounted) {
            setState(() {
              //saveImagePath = null;
              videoController = vcontroller;
            });
          }
          await vcontroller.play();
        }  //startVideoPlayer

Thanks!

1 Answer 1

1

You can call the function from initState(). initState() is called only once when the StatefulWidget is inserted into the Widget tree, so it's a good place to initialize variables or do what you're trying to do.

class _MyHomePageState extends State<MyHomePage> {
 
  @override
  void initState() {
    super.initState();
    // Do anything you need done here
    _startVideoPlayer();

    // If you want a slight delay, use Future.delayed

    Future.delayed(Duration(seconds: 1), (){
        _startVideoPlayer();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
    // rest of app
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.