main.dart 5.3 KB

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