main.dart 5.9 KB

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