9在Swift上实现Nuance语音识别,无法监听onResult,onError ...事件

时间:2015-05-19 13:39:56

标签: ios swift delegates speech-recognition header-files

我有Nuance的Speech Recon项目的两个部分,模块的.h文件(ObjectiveC)和ViewController(swift)。

我想在我的swift SpeechRecognition中设置一个viewController对象,并听取onBegin,onStop ...以及此类方法。

使其编译的唯一方法是使用nil作为委托参数来初始化SpeechRecon对象。显然这不好,因为我的onStart ...和onFinish函数没有触发。

我已经为SKRecogniser文件实现了一个协议,并将我的ViewController类扩展为SKReconDelegate ...但是如果我使用“self”作为初始化对象的委托,编译器会说{{1} }不是有效的类。我知道我需要在两个类之间建立一些委托,但我是一个Android开发人员,我的iOS技能仍然不够清晰。 这是代码,如果我错过了一些重要的部分,请告诉我。 我将非常感谢你的帮助。

UIViewController

以防万一,这是我的Bridge标题:

//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
   }
}

更新 请看下面的解决方案!!

3 个答案:

答案 0 :(得分:7)

这就是我所拥有的 桥接标题:

#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

说到使用所有这些的UIViewController:

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

没有别的,检查每一步,这部分是非常直接的

答案 1 :(得分:0)

尝试let objCDelegate = self as SKRecognizerDelegate,然后使用objCDelegate作为委托参数

答案 2 :(得分:0)

由于事情发生了一些变化,我以为我会增加2美分:

if ($r_select['profile'] != null) {

    $resultSelect = $r_select['profile']; 
    $resultArray  = explode('|', $resultSelect); 

    foreach ($resultArray as $data) {

        if (strpos($data, 'secondset') !== FALSE) {
            $found = $data;
            break;
        }
    }
}
echo $found;

希望这有助于某人!