Muh*_*adi 2 android dart flutter
我正在使用 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,
),
Run Code Online (Sandbox Code Playgroud)
您可以使用 YouTube 嵌入 URL 执行 http 请求并以 JSON 格式获取信息。
https://www.youtube.com/oembed?url=<youtube-video_url_here>&format=json
Run Code Online (Sandbox Code Playgroud)
它包含一些信息,例如缩略图 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;
}
}
Run Code Online (Sandbox Code Playgroud)
示例如何使用该功能:
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'];
Run Code Online (Sandbox Code Playgroud)
在浏览器中打开此链接以查看您在 JSON 中获得的数据:https : //www.youtube.com/oembed? url =https : //www.youtube.com/watch?v= d_m5csmrf7I & format =json
| 归档时间: |
|
| 查看次数: |
2260 次 |
| 最近记录: |