main.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import 'dart:async';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_ijkplayer/flutter_ijkplayer.dart';
  4. import 'package:photo/photo.dart';
  5. import 'package:photo_manager/photo_manager.dart';
  6. void main() => runApp(MyApp());
  7. class MyApp extends StatefulWidget {
  8. @override
  9. _MyAppState createState() => _MyAppState();
  10. }
  11. class _MyAppState extends State<MyApp> {
  12. @override
  13. Widget build(BuildContext context) {
  14. return MaterialApp(
  15. home: HomePage(),
  16. );
  17. }
  18. }
  19. class HomePage extends StatefulWidget {
  20. @override
  21. HomePageState createState() => HomePageState();
  22. }
  23. class HomePageState extends State<HomePage> {
  24. IjkMediaController controller = IjkMediaController();
  25. @override
  26. void initState() {
  27. super.initState();
  28. }
  29. @override
  30. void dispose() {
  31. controller.dispose();
  32. super.dispose();
  33. }
  34. @override
  35. Widget build(BuildContext context) {
  36. return Scaffold(
  37. appBar: AppBar(
  38. title: const Text('Plugin example app'),
  39. actions: <Widget>[
  40. IconButton(
  41. icon: Icon(Icons.videocam),
  42. onPressed: _pickVideo,
  43. ),
  44. ],
  45. ),
  46. body: Container(
  47. // width: MediaQuery.of(context).size.width,
  48. // height: 400,
  49. child: Column(
  50. children: <Widget>[
  51. AspectRatio(
  52. aspectRatio: 1280 / 720,
  53. child: IjkPlayer(
  54. controller: controller,
  55. ),
  56. ),
  57. _buildPlayAssetButton(),
  58. ],
  59. ),
  60. ),
  61. floatingActionButton: FloatingActionButton(
  62. child: Icon(Icons.play_arrow),
  63. onPressed: () async {
  64. await controller.setNetworkDataSource(
  65. // 'https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4',
  66. // 'rtmp://172.16.100.245/live1',
  67. // 'https://www.sample-videos.com/video123/flv/720/big_buck_bunny_720p_10mb.flv',
  68. "https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_1mb.mp4",
  69. // 'http://184.72.239.149/vod/smil:BigBuckBunny.smil/playlist.m3u8',
  70. // "file:///sdcard/Download/Sample1.mp4",
  71. );
  72. print("set data source success");
  73. controller.play();
  74. },
  75. ),
  76. );
  77. }
  78. void _pickVideo() async {
  79. List<AssetEntity> imgList = await PhotoPicker.pickAsset(
  80. // BuildContext required
  81. context: context,
  82. /// The following are optional parameters.
  83. themeColor: Colors.green,
  84. // the title color and bottom color
  85. padding: 1.0,
  86. // item padding
  87. dividerColor: Colors.grey,
  88. // divider color
  89. disableColor: Colors.grey.shade300,
  90. // the check box disable color
  91. itemRadio: 0.88,
  92. // the content item radio
  93. maxSelected: 8,
  94. // max picker image count
  95. // provider: I18nProvider.english,
  96. provider: I18nProvider.chinese,
  97. // i18n provider ,default is chinese. , you can custom I18nProvider or use ENProvider()
  98. rowCount: 3,
  99. // item row count
  100. textColor: Colors.white,
  101. // text color
  102. thumbSize: 160,
  103. // preview thumb size , default is 64
  104. sortDelegate: SortDelegate.common,
  105. // default is common ,or you make custom delegate to sort your gallery
  106. checkBoxBuilderDelegate: DefaultCheckBoxBuilderDelegate(
  107. activeColor: Colors.white,
  108. unselectedColor: Colors.white,
  109. ),
  110. // default is DefaultCheckBoxBuilderDelegate ,or you make custom delegate to create checkbox
  111. badgeDelegate: const DurationBadgeDelegate(),
  112. // badgeDelegate to show badge widget
  113. pickType: PickType.onlyVideo,
  114. );
  115. if (imgList != null && imgList.isNotEmpty) {
  116. var asset = imgList[0];
  117. var fileUri = (await asset.file).uri;
  118. playUri(fileUri.toString());
  119. }
  120. }
  121. void playUri(String uri) async {
  122. await controller.setNetworkDataSource(uri);
  123. print("set data source success");
  124. controller.play();
  125. }
  126. _buildPlayAssetButton() {
  127. return FlatButton(
  128. child: Text("play sample asset"),
  129. onPressed: () async {
  130. await controller.setAssetDataSource("assets/sample1.mp4");
  131. controller.play();
  132. Timer.periodic(Duration(seconds: 2), (timer) async {
  133. var info = await controller.getVideoInfo();
  134. print("info = $info");
  135. if (info == null) {
  136. return;
  137. }
  138. if (info.progress >= 0.95) {
  139. timer.cancel();
  140. }
  141. });
  142. },
  143. );
  144. }
  145. }