插件里面实现了代理方法,然后在代理方法里面进行Native层运行结果的返回,
#import "PGXFYunPlugin.h"
@implementation PGXFYunPlugin
// 开始语音识别
- (void)startListen:(PGMethod *)command
{
if (command) {
// CallBackid 异步方法的回调id,H5+ 会根据回调ID通知JS层运行结果成功或者失败
cbId = [command.arguments objectAtIndex:0];
// 创建语音听写对象
if (iflySpeechRecognizer == nil) {
iflySpeechRecognizer = [IFlySpeechRecognizer sharedInstance];
[iflySpeechRecognizer setParameter:@"" forKey:[IFlySpeechConstant PARAMS]];
// 设置听写参数
[iflySpeechRecognizer setParameter:@"iat" forKey:[IFlySpeechConstant IFLY_DOMAIN]];
//asr_audio_path保存录音文件名,如不再需要,设置value为nil表示取消,默认目录是Library/cache
[iflySpeechRecognizer setParameter:nil forKey:[IFlySpeechConstant ASR_AUDIO_PATH]];
}
iflySpeechRecognizer.delegate = self;
// 启动识别服务
[iflySpeechRecognizer startListening];
}
}
// 停止语音识别
- (void)stopListen:(PGMethod *)command {
if (command) {
if (iflySpeechRecognizer != nil) {
// 停止识别服务
[iflySpeechRecognizer stopListening];
}
}
}
/*
识别结果返回代理
*/
- (void)onResults:(NSArray *)results isLast:(BOOL)isLast
{
NSLog(@"onResults");
if (isLast && cbId != nil) {
// 运行Native代码结果和预期相同,调用回调通知JS层运行成功并返回结果
PDRPluginResult *result = [PDRPluginResult resultWithStatus:PDRCommandStatusOK messageAsString:@"onResults"];
// 通知JS层Native层运行结果,JS Pluginbridge会根据cbid找到对应的回调方法并触发
[self toCallback:cbId withReslut:[result toJSONString]];
}
}
/*
识别会话结束返回代理
errorCode=0,表示正常结束,非0表示发生错误
*/
- (void)onError:(IFlySpeechError *)errorCode
{
NSLog(@"onError");
if (cbId != nil) {
// 如果Native代码运行结果和预期不同,需要通过回调通知JS层出现错误,并返回错误提示
PDRPluginResult *result = [PDRPluginResult resultWithStatus:PDRCommandStatusError messageAsString:@"惨了! 出错了! 咋(wu)整(liao)"];
// 通知JS层Native层运行结果,JS Pluginbridge会根据cbid找到对应的回调方法并触发
[self toCallback:cbId withReslut:[result toJSONString]];
}
}
/*
开始录音回调
*/
- (void)onBeginOfSpeech
{
NSLog(@"onBeginOfSpeech");
}
/*
停止录音回调
*/
- (void)onEndOfSpeech
{
NSLog(@"onEndOfSpeech");
}
@end
报野指针,就是cbId指到空的地方了,要怎么出来?我cbId有用本地变量保存的 ,但是当前调用的方法结束后,这个cbId就被释放了
1 个回复
猫猫猫猫 - 用户已离线
是这样的,你开始录音和停止录音的方法,操作的不是同一个实例。
你js每次调用,都生成了一个新的实例,新的实例并没有开始录音,所以cbId为空。