9在Swift上实现Nuance语音识别,无法侦听onResult,onError ...事件 [英] 9Implementing Nuance Speech Recognition on Swift, cannot listen to onResult, onError... events
问题描述
ViewController
(swift)。 我想在我的快速 viewController $ c中设置一个
SpeechRecognition
$ c>,并听onBegin,onStop ...等方法。
使其编译的唯一方法是使用nil作为委托参数来初始化SpeechRecon对象。显然这不是很好,因为我的onStart ...和onFinish功能不会触发。
我已经实现了一个协议到 SKRecogniser
文件,并将我的ViewController类扩展为SKReconDelegate ...但如果我使用self作为委托来初始化对象,编译器会说 UIViewController
不是一个有效的类。我知道我需要在两个班级之间建立一些代表,但是我是Android开发者,而我的iOS技能还不够锐利。
这是代码,如果我错过了一些重要的事情只是让我知道。
我将非常感谢您的帮助。
// ViewController代码,在SWIFT
//不需要这里的协议!
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)//错误说self是的一个无效的ViewController类型:(因为我没有实现所有4个方法BELOW:
}
//有一点前进,我有一个按钮相同的问题
@IBAction func btnmicaction(sender :AnyObject){
self.voiceSearch = SKRecognizer(type:websearch,detection:2,language:langType as String,delegate:self)//错误说self是无效的ViewController类型:(因为我没有实现所有4种方法BELOW:
}
//实现所有这4个功能,由解决方案建议
func识别器DidBeginRecording(识别器:SKRecognizer) {
println(************** ReconBeganRecording)
}
func识别器DidFinishRecording(识别器:SKRecognizer){
println ************** ReconFinishedRecording)
}
func识别器(识别器:SKRecognizer !, didFinishWithResults结果:SKRecognition!){
//语音识别过程已经理解了一些东西
}
func识别器(识别器:SKRecognizer !, didFinishWithError错误:NSError !,建议:String!){
//一个错误已经发生
}
}
以下是我的Bridge标题:
#ifndef Vanilla_Bridge_h
#define Vanilla_Bridge_h
#import< SpeechKit / SpeechKit.h>
更新
请参阅以下解决方案!!
这是我已经得到
桥接头:
#import< SpeechKit / SpeechKit.h>
#importNuanceHeader.h
NuanceHeader.h:
#import< Foundation / Foundation.h>
@interface NuanceHeader:NSObject
@end
NuanceHeader.m
#importNuanceHeader.h
const unsigned char SpeechKitApplicationKey [] = {...};
@implementation NuanceHeader
@end
当使用UIViewController时所有这些:
class MyViewController:UIViewController,SpeechKitDelegate,SKRecognizerDelegate
{
var voiceSearch:SKRecognizer?
覆盖func viewDidLoad()
{
//安装SpeechKit
SpeechKit.setupWithID(...,主机:sandbox.nmdp.nuancemobility.net ,port:443,useSSL:false,delegate:self)
}
func someAction()
{
self.voiceSearch = SKRecognizer(type:SKSearchRecognizerType,检测:UInt(SKLongEndOfSpeechDetection),语言:eng-USA,委托:self)
}
func识别器DidBeginRecording(识别器:SKRecognizer!)
{
//录音开始
}
func识别器DidFinishRecording(识别器:SKRecognizer!)
{
//录制已停止
}
func识别器(识别器:SKRecognizer !, didFinishWithResults结果:SKRecognition!)
{
//语音识别过程已经了解了
$
func识别器(识别器:SKRecognizer !, didFinishWithError错误:NSError !,建议:String!)
{
//发生错误
}
}
没有什么别的,检查每一步,这部分是很直接的
I have two parts of my Speech Recon project with Nuance, the .h file of a module (ObjectiveC) and aViewController
(swift).
I want to set up aSpeechRecognition
object in my swiftviewController
, and listen to onBegin, onStop... and such methods.
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.
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:
#import <Foundation/Foundation.h>
@interface NuanceHeader : NSObject
@end
NuanceHeader.m
#import "NuanceHeader.h"
const unsigned char SpeechKitApplicationKey[] = {...};
@implementation NuanceHeader
@end
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 ...事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!