Flutter:youtube_player_flutter 无法获取元数据
如何获取视频元数据?
我正在使用 youtube_player_flutter_flutter
当我启动应用程序时,视频播放正确,但是元数据 喜欢' 作者 '或' title '始终从' null '。只有当i'' 热重新加载 '应用程序以正确的值初始化。
我是新手的颤音,我认为这可以通过一些异步功能来解决,但是我不知道应该把它放在哪里
代码:
import 'package:flutter/material.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';
class YoutubePlayerContainer extends StatefulWidget {
const YoutubePlayerContainer({Key? key}) : super(key: key);
@override
State<YoutubePlayerContainer> createState() => _YoutubePlayerContainerState();
}
class _YoutubePlayerContainerState extends State<YoutubePlayerContainer> {
late YoutubePlayerController controller;
@override
void initState() {
super.initState();
const url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
controller = YoutubePlayerController(
initialVideoId: YoutubePlayer.convertUrlToId(url)!,
flags: const YoutubePlayerFlags(),
);
}
@override
Widget build(BuildContext context) => YoutubePlayerBuilder(
player: YoutubePlayer(
controller: controller,
),
builder: (context, player) => Scaffold(
appBar: AppBar(title: const Text('Video MetaData')),
body: Padding(
padding: EdgeInsets.all(8),
child: Column(
children: [
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(3),
color: const Color(0xFF434D58),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(8),
child: Text(
controller.metadata.author,
style: const TextStyle(
fontWeight: FontWeight.w600,
fontSize: 12,
color: Colors.white,
),
),
),
Container(
child: player,
),
Padding(
padding: const EdgeInsets.all(8),
child: Text(
controller.metadata.title,
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 12,
color: Colors.white,
),
),
),
],
),
),
],
),
),
),
);
}
我在这里缺少什么?
How can I fetch the video metadata?
I'm using youtube_player_flutter
When I start the app the video play correctly but the metadata like the 'author' or the 'title' always start at 'null'. Only when I 'Hot Reload' the app they are initialize with the correct value.
I'm new to flutter and I think this can be solved with some asynchronous function but I don't know where I should put it
Code:
import 'package:flutter/material.dart';
import 'package:youtube_player_flutter/youtube_player_flutter.dart';
class YoutubePlayerContainer extends StatefulWidget {
const YoutubePlayerContainer({Key? key}) : super(key: key);
@override
State<YoutubePlayerContainer> createState() => _YoutubePlayerContainerState();
}
class _YoutubePlayerContainerState extends State<YoutubePlayerContainer> {
late YoutubePlayerController controller;
@override
void initState() {
super.initState();
const url = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
controller = YoutubePlayerController(
initialVideoId: YoutubePlayer.convertUrlToId(url)!,
flags: const YoutubePlayerFlags(),
);
}
@override
Widget build(BuildContext context) => YoutubePlayerBuilder(
player: YoutubePlayer(
controller: controller,
),
builder: (context, player) => Scaffold(
appBar: AppBar(title: const Text('Video MetaData')),
body: Padding(
padding: EdgeInsets.all(8),
child: Column(
children: [
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(3),
color: const Color(0xFF434D58),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(8),
child: Text(
controller.metadata.author,
style: const TextStyle(
fontWeight: FontWeight.w600,
fontSize: 12,
color: Colors.white,
),
),
),
Container(
child: player,
),
Padding(
padding: const EdgeInsets.all(8),
child: Text(
controller.metadata.title,
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 12,
color: Colors.white,
),
),
),
],
),
),
],
),
),
),
);
}
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 documentation 对于软件包,可以看到call是异步和异步的和synchonCon的和您需要添加听众。
在您提供的代码中,YouTubePlayer没有听众,因此,第一次查看它,元数据是无效的,因为该调用尚未完成。
我建议仅显示呼叫完成后显示数据。您可以使用布尔国旗并显示旋转器来执行此操作。
Looking at the documentation for the package, it is pretty visible to see that the call is asynchronous and you need to add a listener to it.
In the code you provided, there is no listener for the YoutubePlayer and because of this, the first time it is viewed, the metadata is null since the call has not been completed yet.
I would suggest only showing the data once the call has completed. You could do this using a boolean flag and showing a spinner.