我正在使用youtube_player_flutter:^ 6.1.0 + 4插件播放YouTube视频,如何提取视频的详细信息……如时长和大小
import 'package:youtube_player_flutter/youtube_player_flutter.dart';
YoutubePlayerController _youtubecontroller = YoutubePlayerController():
YoutubePlayer(
controller: _youtubecontroller,
showVideoProgressIndicator: true,
),
答案 0 :(得分:5)
您可以使用YouTube嵌入URL发出http请求,并以JSON获取信息。
https://www.youtube.com/oembed?url=<youtube-video_url_here>&format=json
其中包含一些信息,例如缩略图URL,宽度,高度等。
例如:
import 'package:http/http.dart' as http;
Future<dynamic> getDetail(String userUrl) async {
String embedUrl = "https://www.youtube.com/oembed?url=$userUrl&format=json";
//store http request response to res variable
var res = await http.get(embedUrl);
print("get youtube detail status code: " + res.statusCode.toString());
try {
if (res.statusCode == 200) {
//return the json from the response
return json.decode(res.body);
} else {
//return null if status code other than 200
return null;
}
} on FormatException catch (e) {
print('invalid JSON'+ e.toString());
//return null if error
return null;
}
}
该函数的用法示例:
String videoUrl = 'https://www.youtube.com/watch?v=d_m5csmrf7I';
var jsonData = await getDetail(videoUrl);
//you can take anything provided in the JSON, just change the key according to
//what is available in the response
String title = jsonData['title'];
在浏览器中打开此链接,以查看通过JSON获取的数据: https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v=d_m5csmrf7I&format=json