main.dart 5.9 KB

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