main.dart 5.9 KB

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