测试已测试 | ✗ |
语言语言 | SwiftSwift |
许可证 | MIT |
发布最后发布 | 2016年11月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 ZaidPathan 维护。
要运行示例项目,先克隆仓库,然后从 Example 目录运行 pod install
XCode : 8.0 + iOS : 10.0 + Swift : 3.0 + 设备 : 需要真实的 iOS 设备
SpeechKitManager 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod "SpeechKitManager"
注意:将
NSMicrophoneUsageDescription
(仅用于实时语音到文本) 和NSSpeechRecognitionUsageDescription
添加到info.plist
。
fileprivate var speechKitManager:SpeechKitManager?
let audioPath = Bundle.main.path(forResource: "testAudio", ofType: "m4a") //For audio file to text
var audioURL:URL? //For audio file to text
override func viewDidLoad() {
super.viewDidLoad()
speechKitManager = SpeechKitManager()
}
speechKitManager?.requestSpeechRecognizerAuth { (authStatus) in
/*
The callback may not be called on the main thread. Add an
operation to the main queue to update the record button's state.
*/
OperationQueue.main.addOperation {
switch authStatus {
case .authorized:
self.recognizeAudio()
print("requestSpeechRecognizerAuth authorized")
case .denied:
print("requestSpeechRecognizerAuth denied")
case .restricted:
print("requestSpeechRecognizerAuth restricted")
case .notDetermined:
print("requestSpeechRecognizerAuth notDetermined")
}
}
}
fileprivate func recognizeAudio(){
if let path = self.audioPath{
self.speechKitManager?.recognizeAudio(atURL: URL(fileURLWithPath: path), resultHandler: { (result, error) in
if let result = result{
//Audio to Text fall here
debugPrint(result.bestTranscription.formattedString)
}else if let error = error{
debugPrint(error.localizedDescription)
}
})
}else{
debugPrint("no path found")
}
}
fileprivate func authorizeMicAccess(){
speechKitManager?.requestMicAuth({ (granted) in
if granted{
//Mic access granted start recognition
self.recognize()
}else{
debugPrint("Microphone permission required")
}
})
}
fileprivate func recognize(){
speechKitManager?.record(resultHandler: { (result, error) in
var isFinal = false
if let result = result {
//User speech will fall here to text
debugPrint(result.bestTranscription.formattedString)
isFinal = result.isFinal
}
if error != nil || isFinal {
self.speechKitManager?.stop()
}
})
}
棒极了
Zaid Pathan, [email protected]
SpeechKitManager 凭借 MIT 许可证提供。有关更多信息,请参阅许可证文件。