seekTo() does not work on initalization of State.(flutter)

51 views Asked by At

My code is made to play a youtube video, and what I want to do is to make the movie be played from the time I told it. And of course, the player played from 0 second, which I did not want.

So at below is my code:

class _stretchDetailState extends State<stretchDetail>{
  late YoutubePlayerController _controller;
  @override
  void initState(){
    int skipTo = widget.data_transfer.last;
    _controller = YoutubePlayerController(
      initialVideoId: widget.data_transfer[4],   // Youtube Id 
      flags: YoutubePlayerFlags(
        autoPlay: true,
        mute: false,
      ),

    );
    _controller.seekTo(Duration(seconds: skipTo));  // skipTo is int var that tell from when to start.
  }
  Widget build(BuildContext context) {
//    _controller.seekTo(Duration(seconds: skipTo));
    return Scaffold(
      body:Column(
        children: [
          YoutubePlayer(
            controller: _controller,
            showVideoProgressIndicator: true,
          ),
          Container(
            height:20
          ),

        ],
      )

    );
  }
}

Please anybody tell me what I am missing. Thank you.

1

There are 1 answers

2
Kuroneko On BEST ANSWER

Try putting a adding startAt: skipTo to YoutubePlayerFlags

class _stretchDetailState extends State<stretchDetail>{
  late YoutubePlayerController _controller;
  @override
  void initState(){
    int skipTo = widget.data_transfer.last;
    _controller = YoutubePlayerController(
      initialVideoId: widget.data_transfer[4],   // Youtube Id 
      flags: YoutubePlayerFlags(
        autoPlay: true,
        mute: false,
        startAt: skipTo,//add this
      ),

    );
  }
  Widget build(BuildContext context) {
    return Scaffold(
      body:Column(
        children: [
          YoutubePlayer(
            controller: _controller,
            showVideoProgressIndicator: true,
          ),
          Container(
            height:20
          ),

        ],
      )

    );
  }
}