flutter 插件编写
计划编写一个 flutter 关于音视频控制的插件,借此学习一下插件的编写
第一步 开始创建一个插件
- 启动 Android Studio
- 选择 New Flutter Project 创建插件 (md 没有这个选项了... 什么破文案 )
重来 第一步
ios 项目中创建插件类 接口文件
//
// FlutterAVAudioSessionPlugin.h
// Runner
//
// Created by liuliang on 2022/11/11.
//
#import <Foundation/Foundation.h>
#import <Flutter/Flutter.h>
NS_ASSUME_NONNULL_BEGIN
@interface FlutterAVAudioSessionPlugin : NSObject<FlutterPlugin>
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar;
@end
NS_ASSUME_NONNULL_END
ios 实现类
//
// FlutterAVAudioSessionPlugin.m
// Runner
//
// Created by liuliang on 2022/11/11.
//
#import "FlutterAVAudioSessionPlugin.h"
#import <AVFoundation/AVFoundation.h>
static FlutterMethodChannel *methodChannel;
@implementation FlutterAVAudioSessionPlugin
+ (void)registerWithRegistrar:(nonnull NSObject<FlutterPluginRegistrar> *)registrar{
// 注册通道 启动单利
FlutterMethodChannel* channel = [FlutterMethodChannel methodChannelWithName:@"io.flutter.plugins.avaudio.session" binaryMessenger:[registrar messenger]];
methodChannel = channel;
FlutterAVAudioSessionPlugin* instance = [[FlutterAVAudioSessionPlugin alloc] init];
[registrar addMethodCallDelegate:instance channel:channel];
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
// 如果包含有参数 这样取参
/**
NSString *title = call.arguments[@"title"];
NSString *icon = call.arguments[@"icon"];
NSString *url = call.arguments[@"url"];
*/
// 监听方法 ,** 注意方法命名 驼峰命名法
if ([call.method isEqualToString:@"backAvaudioSession"]) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
}else if ([call.method isEqualToString:@"endBackAvaudioSession"]) {
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
}else {
result(FlutterMethodNotImplemented);
}
// 监听事件的回调
NSLog(@"开启 后台播放");
}
@end
添加 插件到文件桥接文件里面 Runner-Bridging-Header
#import "FlutterAVAudioSessionPlugin.h"
注册到 appDelegate 里面
import UIKit
import Flutter
import WidgetKit
import StoreKit
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
@objc var allowRotation = false;
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
let controller:FlutterViewController = window?.rootViewController as! FlutterViewController
// 初始化MethodChannel,设置监听
FlutterAVAudioSessionPlugin.register(with: self.registrar(forPlugin: "FlutterAVAudioSessionPlugin")!);
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
if allowRotation {
return .all;
}
return .portrait;
}
}
第几步骤来着 , 反正就是 flutter 调用吧
const MethodChannel shareChannel =
MethodChannel('io.flutter.plugins.avaudio.session');
shareChannel.invokeMethod('backAvaudioSession');
网友评论