If you click on a YouTube video besides letting you copy the link, YouTube lets you to copy the link to start from the current time of playing. How can I do that on Flutter with YouTube embedded videos?
I used youtube_player_flutter and used YouTube URLs instead of video IDs, but even if I add a timestamp to the end of the links it still starts from the beginning. Is it possible to do it with this plugin? Is there another plugin capable of doing that?
import 'package:flutter/material.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Example',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
static String videoId = 'BBAyRBTfsOU';
static int startTime = 150;
static String youtubeUrlWithStartTime =
'https://www.youtube.com/watch?v=$videoId&t=$startTime';
final YoutubePlayerController _controller = YoutubePlayerController(
initialVideoId: videoId,
flags: const YoutubePlayerFlags(
autoPlay: true,
mute: false,
),
);
MyHomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter and Youtube'),
),
body: YoutubePlayer(
controller: _controller,
liveUIColor: Colors.amber,
),
);
}
}
2
Answers
You can use the
startAt
property in yourYoutubePlayerFlags
for that.https://pub.dev/documentation/youtube_player_flutter/latest/youtube_player_flutter/YoutubePlayerFlags/startAt.html
Use this example from Flutter’s github page, replace the url of the bee video with the youtube video in question, and then use the query param
t
to set the start_at value. It looks like&t=10
at the end of the share url if you’re starting from 10s, for example.Remove the
main()
function if you already have an app + router/pages set up in your project i.e. you’re just linking to this widget.Hope this helps.
Edit: I realize you’re trying to do more with a
ListView
, but this example should get you started. Sincevideo_player
is actively maintained by Flutter, it may work better thanyoutube_player_flutter
, or at least as-expected (former updated <2 weeks ago, latter >9months ago).