gallery_page.dart 1.8 KB

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