import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:flutter/widgets.dart'; import 'package:permission_handler/permission_handler.dart'; typedef void MofunVideoViewCreatedCallback(MofunVideoViewController controller); const _CHANNEL = 'plugins.shuangyunbang.mofun/mofunvideoview'; class MofunVideoView extends StatefulWidget { final MofunVideoViewCreatedCallback onMofunVideoViewCreated; const MofunVideoView({ Key key, this.onMofunVideoViewCreated, }) : super(key: key); @override State createState() { return _MofunVideoViewState(); } } class _MofunVideoViewState extends State { MofunVideoViewController controller; @override Widget build(BuildContext context) { if (defaultTargetPlatform == TargetPlatform.android) { return AndroidView( viewType: _CHANNEL, onPlatformViewCreated: _onPlatformViewCreated, ); } else if (defaultTargetPlatform == TargetPlatform.iOS) { return UiKitView( viewType: _CHANNEL, onPlatformViewCreated: _onPlatformViewCreated, ); } return Text( '$defaultTargetPlatform is not yet supported by the text_view plugin'); } void _onPlatformViewCreated(int id) { if (controller == null || controller.id != id) { controller = MofunVideoViewController._(id); } if (widget.onMofunVideoViewCreated != null) { widget.onMofunVideoViewCreated(controller); } } } class MofunVideoViewController { final int id; final MethodChannel _channel; bool _hasPermission = false; MofunVideoViewController._(this.id) : _channel = new MethodChannel('${_CHANNEL}_$id'); ///检查权限。返回是否权限充足 Future _checkPermission() async { List needCheck = List(); PermissionHandler permissionHandler = PermissionHandler(); PermissionStatus permission = await permissionHandler.checkPermissionStatus(PermissionGroup.storage); if (permission != PermissionStatus.granted) { needCheck.add(PermissionGroup.storage); } permission = await permissionHandler .checkPermissionStatus(PermissionGroup.microphone); if (permission != PermissionStatus.granted) { needCheck.add(PermissionGroup.microphone); } if (needCheck.isEmpty) { return true; } Map noPermissions = await permissionHandler.requestPermissions(needCheck); noPermissions.removeWhere((key, value) => value == PermissionStatus.granted); return noPermissions.isEmpty; } Future checkPermission() async{ if(!_hasPermission){ return _checkPermission(); } return true; } Future toast(String msg) async { return _channel.invokeMethod('toast', msg); } Future play(String url) async { await checkPermission(); return _channel.invokeMethod('play', url); } Future stop() async { await checkPermission(); return _channel.invokeMethod('stop'); } Future save(String output) async { await checkPermission(); return _channel.invokeMethod('save', output); } Future record() async { await checkPermission(); return _channel.invokeMethod('record'); } Future playerAndResume() async { await checkPermission(); return _channel.invokeMethod('playerAndResume'); } }