main.dart 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. ],
  61. ),
  62. ),
  63. floatingActionButton: FloatingActionButton(
  64. child: Icon(Icons.play_arrow),
  65. onPressed: () async {
  66. await controller.setNetworkDataSource(
  67. // 'http://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4',
  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. Widget buildIjkPlayer() {
  82. return Container(
  83. height: 400,
  84. child: IjkPlayer(
  85. mediaController: controller,
  86. ),
  87. );
  88. }
  89. void _pickVideo() async {
  90. List<AssetEntity> imgList = await PhotoPicker.pickAsset(
  91. // BuildContext required
  92. context: context,
  93. /// The following are optional parameters.
  94. themeColor: Colors.green,
  95. // the title color and bottom color
  96. padding: 1.0,
  97. // item padding
  98. dividerColor: Colors.grey,
  99. // divider color
  100. disableColor: Colors.grey.shade300,
  101. // the check box disable color
  102. itemRadio: 0.88,
  103. // the content item radio
  104. maxSelected: 8,
  105. // max picker image count
  106. // provider: I18nProvider.english,
  107. provider: I18nProvider.chinese,
  108. // i18n provider ,default is chinese. , you can custom I18nProvider or use ENProvider()
  109. rowCount: 3,
  110. // item row count
  111. textColor: Colors.white,
  112. // text color
  113. thumbSize: 160,
  114. // preview thumb size , default is 64
  115. sortDelegate: SortDelegate.common,
  116. // default is common ,or you make custom delegate to sort your gallery
  117. checkBoxBuilderDelegate: DefaultCheckBoxBuilderDelegate(
  118. activeColor: Colors.white,
  119. unselectedColor: Colors.white,
  120. ),
  121. // default is DefaultCheckBoxBuilderDelegate ,or you make custom delegate to create checkbox
  122. badgeDelegate: const DurationBadgeDelegate(),
  123. // badgeDelegate to show badge widget
  124. pickType: PickType.onlyVideo,
  125. );
  126. if (imgList != null && imgList.isNotEmpty) {
  127. var asset = imgList[0];
  128. var file = (await asset.file).absolute;
  129. playFile(file);
  130. }
  131. }
  132. void playFile(File file) async {
  133. await controller.setFileDataSource(file, autoPlay: true);
  134. }
  135. void playUri(String uri) async {
  136. await controller.setNetworkDataSource(uri, autoPlay: true);
  137. }
  138. _buildPlayAssetButton() {
  139. return FlatButton(
  140. child: Text("play sample asset"),
  141. onPressed: () async {
  142. await controller.setAssetDataSource(
  143. "assets/sample1.mp4",
  144. autoPlay: true,
  145. );
  146. Timer.periodic(Duration(seconds: 2), (timer) async {
  147. var info = await controller.getVideoInfo();
  148. print("info = $info");
  149. if (info == null) {
  150. return;
  151. }
  152. if (info.progress >= 0.95) {
  153. timer.cancel();
  154. }
  155. });
  156. },
  157. );
  158. }
  159. _buildControllerButtons() {
  160. return Row(
  161. children: <Widget>[
  162. FlatButton(
  163. child: StreamBuilder<bool>(
  164. stream: controller.playingStream,
  165. initialData: controller?.isPlaying ?? false,
  166. builder: (context, snapshot) {
  167. var isPlaying = snapshot.hasData && snapshot.data;
  168. return Text(isPlaying ? "暂停" : "播放");
  169. },
  170. ),
  171. onPressed: () async {
  172. await controller?.playOrPause();
  173. },
  174. ),
  175. FlatButton(
  176. child: Text("停止"),
  177. onPressed: () async {
  178. await controller?.stop();
  179. },
  180. ),
  181. ],
  182. );
  183. }
  184. _buildVolumeBar() {
  185. return StreamBuilder<int>(
  186. stream: controller?.volumeStream,
  187. initialData: controller?.volume,
  188. builder: (context, snapshot) {
  189. if (!snapshot.hasData) {
  190. return Container();
  191. }
  192. var volume = snapshot.data;
  193. return Slider(
  194. value: volume / 100,
  195. onChanged: (double value) {
  196. var targetVolume = (value * 100).toInt();
  197. controller.volume = targetVolume;
  198. },
  199. );
  200. },
  201. );
  202. }
  203. }