youtube_player_flutter icon indicating copy to clipboard operation
youtube_player_flutter copied to clipboard

How to update initialVideoId?

Open robertnicjoo opened this issue 2 years ago • 1 comments

I am trying to update initialVideoId on list of videos (similar to yourube app, there is list of videos under player and you update player with tapped video) but it does not reload player with new id.

code

controllerFunc(String initVid) {
    setState(() {
      _controller =  YoutubePlayerController(
        initialVideoId: initVid,
        flags: const YoutubePlayerFlags(
          mute: false,
          autoPlay: false,
          disableDragSeek: false,
          loop: false,
          isLive: false,
          forceHD: false,
          enableCaption: true,
        ),
      )..addListener(listener);
      _videoMetaData = const YoutubeMetaData();
      yPlayer();
    });
 }

and I update the initialVideoId on ListTile tap function like:

return ListTile(
  onTap: () {
    setState(() {
      controllerFunc(videosList[index]);
    });
  },
  title: ....,
);

but it does not load new video in player.

robertnicjoo avatar Oct 24 '21 06:10 robertnicjoo

I think you use the YoutubePlayerController.load() method to change videos after the initial load. Here's the example i'm using, note the implementation in floatingActionButton:

class _MyHomePageState extends State<MyHomePage> {
  
  YoutubePlayerController yt = YoutubePlayerController(
    initialVideoId: "FuiafRLTbEQ", //Add videoID.
    flags: YoutubePlayerFlags(
      hideControls: false,
      autoPlay: true,
      mute: false,
    ),
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: YTOldPlayer(yt: yt),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.video_collection),
        onPressed: () {
          setState(() {
            yt.load("rNgqbV3Ht8I");
          });
        },
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
    );
  }
}

cgrant avatar Jan 15 '22 23:01 cgrant