123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- 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<StatefulWidget> createState() {
- return _MofunVideoViewState();
- }
- }
-
- class _MofunVideoViewState extends State<MofunVideoView> {
- 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<bool> _checkPermission() async {
- List<PermissionGroup> 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<PermissionGroup, PermissionStatus> noPermissions =
- await permissionHandler.requestPermissions(needCheck);
- noPermissions.removeWhere((key, value) => value == PermissionStatus.granted);
-
- return noPermissions.isEmpty;
- }
-
- Future<bool> checkPermission() async{
- if(!_hasPermission){
- return _checkPermission();
- }
- return true;
- }
-
- Future<void> toast(String msg) async {
- return _channel.invokeMethod('toast', msg);
- }
-
- Future<void> play(String url) async {
- await checkPermission();
- return _channel.invokeMethod('play', url);
- }
-
- Future<void> stop() async {
- await checkPermission();
- return _channel.invokeMethod('stop');
- }
-
- Future<void> save(String output) async {
- await checkPermission();
- return _channel.invokeMethod('save', output);
- }
-
- Future<void> record() async {
- await checkPermission();
- return _channel.invokeMethod('record');
- }
-
- Future<void> playerAndResume() async {
- await checkPermission();
- return _channel.invokeMethod('playerAndResume');
- }
-
- }
|