youtube_player_flutter
youtube_player_flutter copied to clipboard
[BUG] loop is not working
Even when I set the parameter {loop: true}, the video is still not looping. Is there any way to solve this problem?
Here's my code :
class PlayerWidget1 extends StatefulWidget {
const PlayerWidget1({
Key? key,
this.width,
this.height,
required this.videoId,
required this.start,
required this.end,
}) : super(key: key);
final double? width;
final double? height;
final String videoId;
final double start;
final double end;
@override
_PlayerWidget1State createState() => _PlayerWidget1State();
}
class _PlayerWidget1State extends State<PlayerWidget1> {
late YoutubePlayerController _controller;
@override
void initState() {
super.initState();
_controller = YoutubePlayerController.fromVideoId(
videoId: widget.videoId,
autoPlay: true,
startSeconds: widget.start,
endSeconds: widget.end,
params: const YoutubePlayerParams(
showControls: false,
showFullscreenButton: false,
enableCaption: false,
loop: true,
strictRelatedVideos: true,
),
);
}
@override
Widget build(BuildContext context) {
return YoutubePlayer(
controller: _controller,
aspectRatio: 16 / 9,
);
}
@override
void dispose() {
_controller.close();
super.dispose();
}
},
I also ran into this problem. Apparently the loop functionality only works in conjunction with a playlist (see SO).
EDIT: first solution did not work
Instead of YoutubePlayerController.fromVideoId
you should load a playlist with controller.loadPlaylist
. See example.
I had another issue at this point. In my case the video could not be played if I specified only a single vidoeId. If I specify two, it works.
final controller = YoutubePlayerController(
params: const YoutubePlayerParams(
loop: true, // does not work
),
);
await controller.loadPlaylist(
list: ["dQw4w9WgXcQ", "dQw4w9WgXcQ"],
);
controller.setLoop(loopPlaylists: true); // does work
or
final controller = YoutubePlayerController(
params: const YoutubePlayerParams(
loop: true, // does not work
),
);
controller.loadPlaylist(
list: ["dQw4w9WgXcQ", "dQw4w9WgXcQ"],
).then((value) {
controller.setLoop(loopPlaylists: true); //
});
I also ran into this problem. Apparently the loop functionality only works in conjunction with a playlist (see SO).
EDIT: first solution did not work
Instead of
YoutubePlayerController.fromVideoId
you should load a playlist withcontroller.loadPlaylist
. See example.I had another issue at this point. In my case the video could not be played if I specified only a single vidoeId. If I specify two, it works.
final controller = YoutubePlayerController( params: const YoutubePlayerParams( loop: true, // does not work ), ); await controller.loadPlaylist( list: ["dQw4w9WgXcQ", "dQw4w9WgXcQ"], ); controller.setLoop(loopPlaylists: true); // does work
or
final controller = YoutubePlayerController( params: const YoutubePlayerParams( loop: true, // does not work ), ); controller.loadPlaylist( list: ["dQw4w9WgXcQ", "dQw4w9WgXcQ"], ).then((value) { controller.setLoop(loopPlaylists: true); // });
I sometimes get "An error occurred" when trying to load a video with playlist. Is there anyway to solve this?