Flutter plugin for mofun flutter video.

mofun_video_view.dart 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import 'dart:async';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:flutter/widgets.dart';
  5. import 'package:permission_handler/permission_handler.dart';
  6. typedef void MofunVideoViewCreatedCallback(MofunVideoViewController controller);
  7. const _CHANNEL = 'plugins.shuangyunbang.mofun/mofunvideoview';
  8. class MofunVideoView extends StatefulWidget {
  9. final MofunVideoViewCreatedCallback onMofunVideoViewCreated;
  10. const MofunVideoView({
  11. Key key,
  12. this.onMofunVideoViewCreated,
  13. }) : super(key: key);
  14. @override
  15. State<StatefulWidget> createState() {
  16. return _MofunVideoViewState();
  17. }
  18. }
  19. class _MofunVideoViewState extends State<MofunVideoView> {
  20. MofunVideoViewController controller;
  21. @override
  22. Widget build(BuildContext context) {
  23. if (defaultTargetPlatform == TargetPlatform.android) {
  24. return AndroidView(
  25. viewType: _CHANNEL,
  26. onPlatformViewCreated: _onPlatformViewCreated,
  27. );
  28. } else if (defaultTargetPlatform == TargetPlatform.iOS) {
  29. return UiKitView(
  30. viewType: _CHANNEL,
  31. onPlatformViewCreated: _onPlatformViewCreated,
  32. );
  33. }
  34. return Text(
  35. '$defaultTargetPlatform is not yet supported by the text_view plugin');
  36. }
  37. void _onPlatformViewCreated(int id) {
  38. if (controller == null || controller.id != id) {
  39. controller = MofunVideoViewController._(id);
  40. }
  41. if (widget.onMofunVideoViewCreated != null) {
  42. widget.onMofunVideoViewCreated(controller);
  43. }
  44. }
  45. }
  46. class MofunVideoViewController {
  47. final int id;
  48. final MethodChannel _channel;
  49. bool _hasPermission = false;
  50. MofunVideoViewController._(this.id)
  51. : _channel = new MethodChannel('${_CHANNEL}_$id');
  52. ///检查权限。返回是否权限充足
  53. Future<bool> _checkPermission() async {
  54. List<PermissionGroup> needCheck = List();
  55. PermissionHandler permissionHandler = PermissionHandler();
  56. PermissionStatus permission =
  57. await permissionHandler.checkPermissionStatus(PermissionGroup.storage);
  58. if (permission != PermissionStatus.granted) {
  59. needCheck.add(PermissionGroup.storage);
  60. }
  61. permission = await permissionHandler
  62. .checkPermissionStatus(PermissionGroup.microphone);
  63. if (permission != PermissionStatus.granted) {
  64. needCheck.add(PermissionGroup.microphone);
  65. }
  66. if (needCheck.isEmpty) {
  67. return true;
  68. }
  69. Map<PermissionGroup, PermissionStatus> noPermissions =
  70. await permissionHandler.requestPermissions(needCheck);
  71. noPermissions.removeWhere((key, value) => value == PermissionStatus.granted);
  72. return noPermissions.isEmpty;
  73. }
  74. Future<bool> checkPermission() async{
  75. if(!_hasPermission){
  76. return _checkPermission();
  77. }
  78. return true;
  79. }
  80. Future<void> toast(String msg) async {
  81. return _channel.invokeMethod('toast', msg);
  82. }
  83. Future<void> play(String url) async {
  84. await checkPermission();
  85. return _channel.invokeMethod('play', url);
  86. }
  87. Future<void> stop() async {
  88. await checkPermission();
  89. return _channel.invokeMethod('stop');
  90. }
  91. Future<void> save(String output) async {
  92. await checkPermission();
  93. return _channel.invokeMethod('save', output);
  94. }
  95. Future<void> record() async {
  96. await checkPermission();
  97. return _channel.invokeMethod('record');
  98. }
  99. Future<void> playerAndResume() async {
  100. await checkPermission();
  101. return _channel.invokeMethod('playerAndResume');
  102. }
  103. }