gallery_page.dart 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import 'dart:io';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_ijkplayer/flutter_ijkplayer.dart';
  4. import 'package:ijkplayer_example/i18n/i18n.dart';
  5. import 'package:photo/photo.dart';
  6. class PlayGalleryPage extends StatefulWidget {
  7. @override
  8. _PlayGalleryPageState createState() => _PlayGalleryPageState();
  9. }
  10. class _PlayGalleryPageState extends State<PlayGalleryPage> {
  11. IjkMediaController mediaController = IjkMediaController();
  12. File file;
  13. @override
  14. void initState() {
  15. super.initState();
  16. }
  17. @override
  18. void dispose() {
  19. mediaController.dispose();
  20. super.dispose();
  21. }
  22. @override
  23. Widget build(BuildContext context) {
  24. return Scaffold(
  25. appBar: AppBar(
  26. title: Text(currentI18n.photoButton),
  27. ),
  28. body: ListView(
  29. children: <Widget>[
  30. FlatButton(
  31. child: Text("Pick"),
  32. onPressed: _pickVideo,
  33. ),
  34. _buildFileText(),
  35. Container(
  36. height: 400,
  37. child: IjkPlayer(
  38. mediaController: mediaController,
  39. ),
  40. ),
  41. ],
  42. ),
  43. );
  44. }
  45. _buildFileText() {
  46. if (file?.existsSync() == true) {
  47. return Padding(
  48. padding: const EdgeInsets.all(8.0),
  49. child: Text(
  50. "file.path: ${file.path}",
  51. textAlign: TextAlign.center,
  52. ),
  53. );
  54. }
  55. return Container();
  56. }
  57. void _pickVideo() async {
  58. var assetList = await PhotoPicker.pickAsset(
  59. context: context,
  60. pickType: PickType.onlyVideo,
  61. maxSelected: 1,
  62. );
  63. if (assetList?.isNotEmpty == true) {
  64. var asset = assetList[0];
  65. this.file = await asset.file;
  66. _playVideo();
  67. }
  68. }
  69. void _playVideo() async {
  70. if (this.file != null && this.file.existsSync())
  71. await mediaController.setFileDataSource(
  72. file,
  73. autoPlay: true,
  74. );
  75. }
  76. }