在使用Flutter视频播放组件时,最开始我使用了官方组件video_player
,但是在使用时遇到了报错,查看Github和Google并没有解决,于是我在Flutter组件仓库中查找,最后选择了使用flutter_ijkplayer
。
选择原因
简介
GitHub地址:https://github.com/CaiJingLong/flutter_ijkplayer
使用
引入
1 2 3
| # pubspec.yaml dependencies: flutter_ijkplayer: ${lastes_version}
|
页面使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
| import 'package:flutter/material.dart'; import 'package:flutter_ijkplayer/flutter_ijkplayer.dart';
class VideoInfoPage extends StatefulWidget {
@override State<StatefulWidget> createState() => VideoInfoState(); }
class VideoInfoState extends State<VideoInfoPage> {
String selectPlayUrl = '';
IjkMediaController controller = IjkMediaController();
@override void initState() { super.initState(); getVideoInfo(); }
@override void dispose() { controller.dispose(); super.dispose(); }
@override Widget build(BuildContext context) {
return Scaffold( appBar: AppBar( title: Text('影片信息'), ), body: Container( child: Stack( children: <Widget>[ Positioned( top: 0, left: 0.0, right: 0.0, height: MediaQuery.of(context).size.width / 16 * 9, child: Container( height: MediaQuery.of(context).size.width / 16 * 9, child: IjkPlayer(mediaController: controller), ), ), ] ), ), ); }
getVideoInfo() async { String palyUrl = 'https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4'; setState(() { selectPlayUrl = palyUrl; });
await controller.setNetworkDataSource( palyUrl, autoPlay: true ); } }
|
切换来源
可以为按钮添加点击事件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| onClick: () async { String playUrl = 'xxx'; setState(() { selectPlayUrl = playUrl; }); await controller.reset(); controller.setNetworkDataSource( playUrl, autoPlay: false ); },
|
更多介绍请详见官方文档
效果展示