Flutter 视频播放video_player、chewie
1.官方插件video_player
引入插件
dependencies:video_player: ^2.10.0
class MyState extends State {late VideoPlayerController vpc;late Future future;void initState() {//视频初始化vpc = VideoPlayerController.networkUrl(Uri.parse("https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/360/Big_Buck_Bunny_360_10s_1MB.mp4",),);future = vpc.initialize();} Widget build(BuildContext context) {double width = MediaQuery.of(context).size.width;double height = MediaQuery.of(context).size.height / 3;Widget widget = Column(children: [Container(width: width,height: height,child: AspectRatio(aspectRatio: vpc.value.aspectRatio,//定义宽高比child: VideoPlayer(vpc),),),Row(mainAxisAlignment: MainAxisAlignment.spaceAround,children: [IconButton(onPressed: () {vpc.play();},icon: Icon(Icons.play_circle),),IconButton(onPressed: () {vpc.pause();},icon: Icon(Icons.pause_circle),),IconButton(onPressed: () {vpc.seekTo(Duration(seconds: 0));vpc.play();},icon: Icon(Icons.replay),),],),],);return Scaffold(appBar: AppBar(title: Text(""), centerTitle: true),body: widget,);}
}
2.chewie
第三方视频播放插件,增加了控制栏和全屏显示功能。
引入插件
dependencies:chewie: ^1.12.1
class MyState extends State {late VideoPlayerController vpc;late Future future;void initState() {//视频初始化vpc = VideoPlayerController.networkUrl(Uri.parse("https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/360/Big_Buck_Bunny_360_10s_1MB.mp4",),);future = vpc.initialize();} Widget build(BuildContext context) {ChewieController chewieController = ChewieController(videoPlayerController: vpc,aspectRatio: 16 / 9,autoPlay: true,);Widget cw = Center(child: Chewie(controller: chewieController));return Scaffold(appBar: AppBar(title: Text(""), centerTitle: true),body: Container(child: cw,height: 300,width: MediaQuery.of(context).size.width,),);}
}