TextField通过语音输入(语音到文本)?

时间:2015-07-17 05:28:20

标签: ios swift uitextfield speech-recognition speech-to-text

我正在使用Swift和Xcode 6构建iOS应用程序。我想在我的应用程序中实现语音到文本功能。

我用Google搜索并找到了一些链接,但这些链接没有帮助,有些链接在Objectivec中,就像openEars一样。

我有两到三个文本字段,用户可以输入他/她的姓名,年龄和位置。并且在文本字段中还有用于语音输入的麦克风按钮。 如下图所示。 enter image description here

有人可以帮助我如何使用Swift实现此功能。

感谢帮助! 提前谢谢!

1 个答案:

答案 0 :(得分:2)

您可以在swift项目中以这种方式实现openEars:

首先添加您从here下载的框架。

<强>桥接-Header.h

#import <OpenEars/OELanguageModelGenerator.h>
#import <OpenEars/OEAcousticModel.h>
#import <OpenEars/OEPocketsphinxController.h>
#import <OpenEars/OEEventsObserver.h>
#import <OpenEars/OELogging.h>
#import <OpenEars/OEFliteController.h>
#import <Slt/Slt.h>

<强> ViewController.swift

//
//  ViewController.swift
//  SpeechToText
//
//  Created by Anil on 08/07/15.
//  Copyright (c) 2015 Variya Soft Solutions. All rights reserved.
//

import UIKit

var lmPath: String!
var dicPath: String!
var words: Array<String> = []
var currentWord: String!

var kLevelUpdatesPerSecond = 18

class ViewController: UIViewController, OEEventsObserverDelegate {

    var openEarsEventsObserver = OEEventsObserver()
    var startupFailedDueToLackOfPermissions = Bool()

    var buttonFlashing = false

    @IBOutlet weak var recordButton: UIButton!
    @IBOutlet weak var heardTextView: UITextView!
    @IBOutlet weak var statusTextView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
        loadOpenEars()
    }

    @IBAction func record(sender: AnyObject) {

        if !buttonFlashing {
            startFlashingbutton()
            startListening()
        } else {
            stopFlashingbutton()
            stopListening()
        }
    }

    func startFlashingbutton() {

        buttonFlashing = true
        recordButton.alpha = 1

        UIView.animateWithDuration(0.5 , delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut | UIViewAnimationOptions.Repeat | UIViewAnimationOptions.Autoreverse | UIViewAnimationOptions.AllowUserInteraction, animations: {

            self.recordButton.alpha = 0.1

            }, completion: {Bool in
        })
    }

    func stopFlashingbutton() {

        buttonFlashing = false

        UIView.animateWithDuration(0.1, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut | UIViewAnimationOptions.BeginFromCurrentState, animations: {

            self.recordButton.alpha = 1

            }, completion: {Bool in
        })
    }
    //OpenEars methods begin

    func loadOpenEars() {

        self.openEarsEventsObserver = OEEventsObserver()
        self.openEarsEventsObserver.delegate = self

        var lmGenerator: OELanguageModelGenerator = OELanguageModelGenerator()

        addWords()
        var name = "LanguageModelFileStarSaver"
        lmGenerator.generateLanguageModelFromArray(words, withFilesNamed: name, forAcousticModelAtPath: OEAcousticModel.pathToModel("AcousticModelEnglish"))

        lmPath = lmGenerator.pathToSuccessfullyGeneratedLanguageModelWithRequestedName(name)
        dicPath = lmGenerator.pathToSuccessfullyGeneratedDictionaryWithRequestedName(name)
    }


    func pocketsphinxDidStartListening() {
        println("Pocketsphinx is now listening.")
        statusTextView.text = "Pocketsphinx is now listening."
    }

    func pocketsphinxDidDetectSpeech() {
        println("Pocketsphinx has detected speech.")
        statusTextView.text = "Pocketsphinx has detected speech."
    }

    func pocketsphinxDidDetectFinishedSpeech() {
        println("Pocketsphinx has detected a period of silence, concluding an utterance.")
        statusTextView.text = "Pocketsphinx has detected a period of silence, concluding an utterance."
    }

    func pocketsphinxDidStopListening() {
        println("Pocketsphinx has stopped listening.")
        statusTextView.text = "Pocketsphinx has stopped listening."
    }

    func pocketsphinxDidSuspendRecognition() {
        println("Pocketsphinx has suspended recognition.")
        statusTextView.text = "Pocketsphinx has suspended recognition."
    }

    func pocketsphinxDidResumeRecognition() {
        println("Pocketsphinx has resumed recognition.")
        statusTextView.text = "Pocketsphinx has resumed recognition."
    }

    func pocketsphinxDidChangeLanguageModelToFile(newLanguageModelPathAsString: String, newDictionaryPathAsString: String) {
        println("Pocketsphinx is now using the following language model: \(newLanguageModelPathAsString) and the following dictionary: \(newDictionaryPathAsString)")
    }

    func pocketSphinxContinuousSetupDidFailWithReason(reasonForFailure: String) {
        println("Listening setup wasn't successful and returned the failure reason: \(reasonForFailure)")
        statusTextView.text = "Listening setup wasn't successful and returned the failure reason: \(reasonForFailure)"
    }

    func pocketSphinxContinuousTeardownDidFailWithReason(reasonForFailure: String) {
        println("Listening teardown wasn't successful and returned the failure reason: \(reasonForFailure)")
        statusTextView.text = "Listening teardown wasn't successful and returned the failure reason: \(reasonForFailure)"
    }

    func testRecognitionCompleted() {
        println("A test file that was submitted for recognition is now complete.")
        statusTextView.text = "A test file that was submitted for recognition is now complete."
    }

    func startListening() {
        OEPocketsphinxController.sharedInstance().setActive(true, error: nil)
        OEPocketsphinxController.sharedInstance().startListeningWithLanguageModelAtPath(lmPath, dictionaryAtPath: dicPath, acousticModelAtPath: OEAcousticModel.pathToModel("AcousticModelEnglish"), languageModelIsJSGF: false)
    }

    func stopListening() {
        OEPocketsphinxController.sharedInstance().stopListening()
    }

    func addWords() {
        //add any thing here that you want to be recognized. Must be in capital letters
        words.append("SUNDAY")
        words.append("MONDAY")
        words.append("TUESDAY")
        words.append("WEDNESDAY")
        words.append("THURSDAY")
        words.append("FRIDAY")
        words.append("SATURDAY")

        words.append("JANUARY")
        words.append("FEBRUARY")
        words.append("MARCH")
        words.append("APRIL")
        words.append("MAY")
        words.append("JUNE")
        words.append("JULY")
        words.append("AUGUST")
        words.append("SEPTEMBER")
        words.append("OCTOBER")
        words.append("NOVEMBER")
        words.append("DECEMBER")
    }

    func getNewWord() {
        var randomWord = Int(arc4random_uniform(UInt32(words.count)))
        currentWord = words[randomWord]
    }

    func pocketsphinxFailedNoMicPermissions() {

        NSLog("Local callback: The user has never set mic permissions or denied permission to this app's mic, so listening will not start.")
        self.startupFailedDueToLackOfPermissions = true
        if OEPocketsphinxController.sharedInstance().isListening {
            var error = OEPocketsphinxController.sharedInstance().stopListening() // Stop listening if we are listening.
            if(error != nil) {
                NSLog("Error while stopping listening in micPermissionCheckCompleted: %@", error);
            }
        }
    }

    func pocketsphinxDidReceiveHypothesis(hypothesis: String!, recognitionScore: String!, utteranceID: String!) {

        heardTextView.text = "Heard: \(hypothesis)"
    }
}

在这段代码中我添加了一个按钮。按下该按钮即可发言,并且lib将识别您的单词。

请记住,OpenEars可以识别的一件事只是将单词添加到数组中而您无法识别其他单词。

您可以测试THIS示例项目。

希望这会有所帮助。