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