main.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_ijkplayer/flutter_ijkplayer.dart';
  3. import 'package:photo/photo.dart';
  4. void main() => runApp(MyApp());
  5. class MyApp extends StatefulWidget {
  6. @override
  7. _MyAppState createState() => _MyAppState();
  8. }
  9. class _MyAppState extends State<MyApp> {
  10. @override
  11. Widget build(BuildContext context) {
  12. return MaterialApp(
  13. home: HomePage(),
  14. );
  15. }
  16. }
  17. class HomePage extends StatefulWidget {
  18. @override
  19. HomePageState createState() => HomePageState();
  20. }
  21. class HomePageState extends State<HomePage> {
  22. IjkMediaController controller = IjkMediaController();
  23. @override
  24. void initState() {
  25. super.initState();
  26. }
  27. @override
  28. void dispose() {
  29. controller.dispose();
  30. super.dispose();
  31. }
  32. @override
  33. Widget build(BuildContext context) {
  34. return Scaffold(
  35. appBar: AppBar(
  36. title: const Text('Plugin example app'),
  37. actions: <Widget>[
  38. IconButton(
  39. icon: Icon(Icons.videocam),
  40. onPressed: _pickVideo,
  41. ),
  42. ],
  43. ),
  44. body: Container(
  45. // width: MediaQuery.of(context).size.width,
  46. // height: 400,
  47. child: Column(
  48. children: <Widget>[
  49. AspectRatio(
  50. aspectRatio: 1280 / 720,
  51. child: IjkPlayer(
  52. controller: controller,
  53. ),
  54. ),
  55. ],
  56. ),
  57. ),
  58. floatingActionButton: FloatingActionButton(
  59. child: Icon(Icons.play_arrow),
  60. onPressed: () async {
  61. await controller.setDataSource(
  62. // 'https://www.sample-videos.com/video123/mp4/720/big_buck_bunny_720p_20mb.mp4',
  63. 'rtmp://172.16.100.245/live1',
  64. // 'https://www.sample-videos.com/video123/flv/720/big_buck_bunny_720p_10mb.flv',
  65. // 'http://184.72.239.149/vod/smil:BigBuckBunny.smil/playlist.m3u8',
  66. // "file:///sdcard/Download/Sample1.mp4",
  67. );
  68. print("set data source success");
  69. controller.play();
  70. },
  71. ),
  72. );
  73. }
  74. void _pickVideo() async {
  75. var list = await PhotoPicker.pickAsset(
  76. context: context,
  77. pickType: PickType.onlyVideo,
  78. );
  79. if (list != null && list.isNotEmpty) {
  80. var asset = list[0];
  81. var fileUri = (await asset.file).uri;
  82. playUri(fileUri.toString());
  83. }
  84. }
  85. void playUri(String uri) async {
  86. await controller.setDataSource(uri);
  87. print("set data source success");
  88. controller.play();
  89. }
  90. }