main.dart 5.9 KB

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