main.dart 4.3 KB

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