我正在使用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,
),
您可以使用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<Video> 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'); //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'];