9Implementing Nuance Speech Recognition on Swift, cannot listen to onResult, onError... events(9在Swift上实现Nuance语音识别,无法监听onResult、onError...事件)
问题描述
我的 Speech Recon 项目有两部分与 Nuance,模块的 .h 文件 (ObjectiveC) 和 ViewController
(swift).
I have two parts of my Speech Recon project with Nuance, the .h file of a module (ObjectiveC) and aViewController
(swift).
我想在我的swiftviewController
中设置一个SpeechRecognition
对象,并监听onBegin、onStop...等方法.
I want to set up aSpeechRecognition
object in my swiftviewController
, and listen to onBegin, onStop... and such methods.
使其编译的唯一方法是使用 nil 作为委托参数来初始化 SpeechRecon 对象.显然这不好,因为我的 onStart... 和 onFinish 函数不会触发.
The only way to make it compile is to use nil as the delegate parameter to initialize the SpeechRecon object. Obviously this is not good because my onStart... and onFinish functions don´t trigger.
我已经为 SKRecogniser
文件实现了一个协议,并将我的 ViewController 类扩展到 SKReconDelegate...但是如果我使用self"作为委托来初始化对象,编译器会说UIViewController
不是一个有效的类.我知道我需要在两个类之间建立一些委托,但我是一名 android 开发人员,我的 iOS 技能仍然不够敏锐.这是代码,如果我错过了一些重要的部分,请告诉我.我将非常感谢您的帮助.
I have implemented a protocol to theSKRecogniser
file, and extended my ViewController class to SKReconDelegate... but if I use "self" as a delegate to initialize object, the compiler will say thatUIViewController
is not a valid class. I know I need to establish some delegate between both classes, but I am an android developers, and my iOS skills are still not sharp enough.
Here is the code, if I missed some important piece just let me know.
I will be very thankful for your help.
//ViewController code, in SWIFT
//NO PROTOCOLS NEEDED HERE!
class ViewController: UIViewController, SpeechKitDelegate, SKRecognizerDelegate{
override func viewDidLoad() {
super.viewDidLoad()
SpeechKit.setupWithID( "NMDPTRIAL_nuance_chch_com9999",
host:"sandbox.nmdp.nuancemility.net",
port:443,
useSSL:false,
delegate:self) //error said "self" is of an invalid ViewController type :( because I was NOT implementing all 4 methods BELOW:
}
//a bit ahead, I have the same problem with a button
@IBAction func btnmicaction(sender: AnyObject) {
self.voiceSearch=SKRecognizer(type: "websearch", detection: 2, language: langType as String, delegate: self) //error said "self" is of an invalid ViewController type :( because I was NOT implementing all 4 methods BELOW:
}
//IMPLEMENT ALL THESE 4 FUNCTIONS, AS SUGGESTED BY THE SOLUTION
func recognizerDidBeginRecording(recognizer:SKRecognizer){
println("************** ReconBeganRecording")
}
func recognizerDidFinishRecording(recognizer:SKRecognizer){
println("************** ReconFinishedRecording")
}
func recognizer(recognizer: SKRecognizer!, didFinishWithResults results: SKRecognition!){
//The voice recognition process has understood something
}
func recognizer(recognizer: SKRecognizer!, didFinishWithError error: NSError!, suggestion: String!){
//an error has occurred
}
}
以防万一,这是我的桥头:
Just in case, here is my Bridge header:
#ifndef Vanilla_Bridge_h
#define Vanilla_Bridge_h
#import <SpeechKit/SpeechKit.h>
更新请参阅下面的解决方案!
UPDATE SEE SOLUTION BELOW!!
推荐答案
这就是我得到的桥接头:
Here's what I've got Bridging Header:
#import <SpeechKit/SpeechKit.h>
#import "NuanceHeader.h"
NuanceHeader.h:
NuanceHeader.h:
#import <Foundation/Foundation.h>
@interface NuanceHeader : NSObject
@end
NuanceHeader.m
NuanceHeader.m
#import "NuanceHeader.h"
const unsigned char SpeechKitApplicationKey[] = {...};
@implementation NuanceHeader
@end
说到使用这一切的 UIViewController:
When it comes to the UIViewController that uses all this:
class MyViewController: UIViewController, SpeechKitDelegate, SKRecognizerDelegate
{
var voiceSearch: SKRecognizer?
override func viewDidLoad()
{
//Setup SpeechKit
SpeechKit.setupWithID("...", host: "sandbox.nmdp.nuancemobility.net", port: 443, useSSL: false, delegate: self)
}
func someAction()
{
self.voiceSearch = SKRecognizer(type: SKSearchRecognizerType, detection: UInt(SKLongEndOfSpeechDetection), language:"eng-USA", delegate: self)
}
func recognizerDidBeginRecording(recognizer: SKRecognizer!)
{
//The recording has started
}
func recognizerDidFinishRecording(recognizer: SKRecognizer!)
{
//The recording has stopped
}
func recognizer(recognizer: SKRecognizer!, didFinishWithResults results: SKRecognition!)
{
//The voice recognition process has understood something
}
func recognizer(recognizer: SKRecognizer!, didFinishWithError error: NSError!, suggestion: String!)
{
//an error has occurred
}
}
没有别的了,检查每一步,这部分很简单
There is nothing else to it, check every step, this part is pretty straight forward
这篇关于9在Swift上实现Nuance语音识别,无法监听onResult、onError...事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:9在Swift上实现Nuance语音识别,无法监听onResult、onError...事件


基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01