如何在flutter中播放youtube视频?
我试了两个插件,但其中有一些错误。
youtube_player插件(https://pub.dev/packages/youtube_player)给出了一些视频的exoPlayer错误&这些视频不是使用此插件播放的(请参阅https://github.com/sarbagyastha/youtube_player/issues/42)。
youtube_player_flutter插件(https://pub.dev/packages/youtube_player_flutter)有严重的错误&它现在不能使用。
还有其他选择吗?
您可以尝试使用video_player(https://pub.dev/packages/video_player)
import 'package:video_player/video_player.dart';
import 'package:flutter/material.dart';
void main() => runApp(VideoApp());
class VideoApp extends StatefulWidget {
@override
_VideoAppState createState() => _VideoAppState();
}
class _VideoAppState extends State<VideoApp> {
VideoPlayerController _controller;
@override
void initState() {
super.initState();
_controller = VideoPlayerController.network(
'youtubevideolink')
..initialize().then((_) {
setState(() {
// do stuff
});
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Video Demo',
home: Scaffold(
body: Center(
child: _controller.value.initialized
? AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: VideoPlayer(_controller),
)
: Container(),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
setState(() {
_controller.value.isPlaying
? _controller.pause()
: _controller.play();
});
},
child: Icon(
_controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
),
),
),
);
}
@override
void dispose() {
super.dispose();
_controller.dispose();
}
}