Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

ios - 9Implementing Nuance Speech Recognition on Swift, cannot listen to onResult, onError... events

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!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...