youtube_player_flutter
youtube_player_flutter copied to clipboard
How to update initialVideoId?
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.
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,
);
}
}