main.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_ijkplayer/flutter_ijkplayer.dart';
  5. import 'package:photo/photo.dart';
  6. import 'package:photo_manager/photo_manager.dart';
  7. void main() {
  8. IjkConfig.isLog = true;
  9. IjkManager.initIJKPlayer();
  10. runApp(MyApp());
  11. }
  12. class MyApp extends StatefulWidget {
  13. @override
  14. _MyAppState createState() => _MyAppState();
  15. }
  16. class _MyAppState extends State<MyApp> {
  17. @override
  18. Widget build(BuildContext context) {
  19. return MaterialApp(
  20. home: HomePage(),
  21. );
  22. }
  23. }
  24. class HomePage extends StatefulWidget {
  25. @override
  26. HomePageState createState() => HomePageState();
  27. }
  28. class HomePageState extends State<HomePage> {
  29. IjkMediaController controller = IjkMediaController();
  30. @override
  31. void initState() {
  32. super.initState();
  33. }
  34. @override
  35. void dispose() {
  36. controller.dispose();
  37. super.dispose();
  38. }
  39. @override
  40. Widget build(BuildContext context) {
  41. return Scaffold(
  42. appBar: AppBar(
  43. title: const Text('Plugin example app'),
  44. actions: <Widget>[
  45. IconButton(
  46. icon: Icon(Icons.videocam),
  47. onPressed: _pickVideo,
  48. ),
  49. ],
  50. ),
  51. body: Container(
  52. // width: MediaQuery.of(context).size.width,
  53. // height: 400,
  54. child: ListView(
  55. children: <Widget>[
  56. buildIjkPlayer(),
  57. _buildPlayAssetButton(),
  58. _buildControllerButtons(),
  59. _buildVolumeBar(),
  60. _buildSystemVolumeButton(),
  61. ],
  62. ),
  63. ),
  64. floatingActionButton: FloatingActionButton(
  65. child: Icon(Icons.play_arrow),
  66. onPressed: () async {
  67. await controller.setNetworkDataSource(
  68. // 'http://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4',
  69. 'https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4',
  70. // 'rtmp://172.16.100.245/live1',
  71. // 'https://www.sample-videos.com/video123/flv/720/big_buck_bunny_720p_10mb.flv',
  72. // "https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4",
  73. // 'http://184.72.239.149/vod/smil:BigBuckBunny.smil/playlist.m3u8',
  74. // "file:///sdcard/Download/Sample1.mp4",
  75. autoPlay: true);
  76. print("set data source success");
  77. // controller.playOrPause();
  78. },
  79. ),
  80. );
  81. }
  82. Widget buildIjkPlayer() {
  83. return Container(
  84. height: 400,
  85. child: IjkPlayer(
  86. mediaController: controller,
  87. ),
  88. );
  89. }
  90. void _pickVideo() async {
  91. List<AssetEntity> imgList = await PhotoPicker.pickAsset(
  92. // BuildContext required
  93. context: context,
  94. /// The following are optional parameters.
  95. themeColor: Colors.green,
  96. // the title color and bottom color
  97. padding: 1.0,
  98. // item padding
  99. dividerColor: Colors.grey,
  100. // divider color
  101. disableColor: Colors.grey.shade300,
  102. // the check box disable color
  103. itemRadio: 0.88,
  104. // the content item radio
  105. maxSelected: 8,
  106. // max picker image count
  107. // provider: I18nProvider.english,
  108. provider: I18nProvider.chinese,
  109. // i18n provider ,default is chinese. , you can custom I18nProvider or use ENProvider()
  110. rowCount: 3,
  111. // item row count
  112. textColor: Colors.white,
  113. // text color
  114. thumbSize: 160,
  115. // preview thumb size , default is 64
  116. sortDelegate: SortDelegate.common,
  117. // default is common ,or you make custom delegate to sort your gallery
  118. checkBoxBuilderDelegate: DefaultCheckBoxBuilderDelegate(
  119. activeColor: Colors.white,
  120. unselectedColor: Colors.white,
  121. ),
  122. // default is DefaultCheckBoxBuilderDelegate ,or you make custom delegate to create checkbox
  123. badgeDelegate: const DurationBadgeDelegate(),
  124. // badgeDelegate to show badge widget
  125. pickType: PickType.onlyVideo,
  126. );
  127. if (imgList != null && imgList.isNotEmpty) {
  128. var asset = imgList[0];
  129. var file = (await asset.file).absolute;
  130. playFile(file);
  131. }
  132. }
  133. void playFile(File file) async {
  134. await controller.setFileDataSource(file, autoPlay: true);
  135. }
  136. void playUri(String uri) async {
  137. await controller.setNetworkDataSource(uri, autoPlay: true);
  138. }
  139. _buildPlayAssetButton() {
  140. return FlatButton(
  141. child: Text("play sample asset"),
  142. onPressed: () async {
  143. await controller.setAssetDataSource(
  144. "assets/sample1.mp4",
  145. autoPlay: true,
  146. );
  147. Timer.periodic(Duration(seconds: 2), (timer) async {
  148. var info = await controller.getVideoInfo();
  149. print("info = $info");
  150. if (info == null) {
  151. return;
  152. }
  153. if (info.progress >= 0.95) {
  154. timer.cancel();
  155. }
  156. });
  157. },
  158. );
  159. }
  160. _buildControllerButtons() {
  161. return Row(
  162. children: <Widget>[
  163. FlatButton(
  164. child: StreamBuilder<bool>(
  165. stream: controller.playingStream,
  166. initialData: controller?.isPlaying ?? false,
  167. builder: (context, snapshot) {
  168. var isPlaying = snapshot.hasData && snapshot.data;
  169. return Text(isPlaying ? "暂停" : "播放");
  170. },
  171. ),
  172. onPressed: () async {
  173. await controller?.playOrPause();
  174. },
  175. ),
  176. FlatButton(
  177. child: Text("停止"),
  178. onPressed: () async {
  179. await controller?.stop();
  180. },
  181. ),
  182. ],
  183. );
  184. }
  185. _buildVolumeBar() {
  186. return StreamBuilder<int>(
  187. stream: controller?.volumeStream,
  188. initialData: controller?.volume,
  189. builder: (context, snapshot) {
  190. if (!snapshot.hasData) {
  191. return Container();
  192. }
  193. var volume = snapshot.data;
  194. return Slider(
  195. value: volume / 100,
  196. onChanged: (double value) {
  197. var targetVolume = (value * 100).toInt();
  198. controller.volume = targetVolume;
  199. },
  200. );
  201. },
  202. );
  203. }
  204. _buildSystemVolumeButton() {
  205. return FlatButton(
  206. child: Text("显示系统音量"),
  207. onPressed: () async {
  208. var systemVolume = await IjkManager.getSystemVolume();
  209. print(systemVolume);
  210. },
  211. );
  212. }
  213. }