|
1 |
| -import UIKit |
2 | 1 | import Flutter
|
3 | 2 | import Speech
|
| 3 | +import UIKit |
| 4 | + |
4 | 5 | @UIApplicationMain
|
5 | 6 | @objc class AppDelegate: FlutterAppDelegate {
|
6 |
| - |
7 | 7 | private func receiveTxRequest(call: FlutterMethodCall, result: @escaping FlutterResult) {
|
8 |
| - let uri = (call.arguments as! [String:Any])["path"] as! String |
9 |
| - let url = URL(fileURLWithPath: uri) |
10 |
| - guard let myRecognizer = SFSpeechRecognizer() else { |
11 |
| - // A recognizer is not supported for the current locale |
12 |
| - result(FlutterError(code: "FAILED_REC", message: "unsupported locale", details: nil)) |
13 |
| - return |
14 |
| - } |
15 |
| - |
16 |
| - if !myRecognizer.isAvailable { |
17 |
| - result(FlutterError(code: "FAILED_REC", message: "unavailable recognizer", details: nil)) |
18 |
| - return |
19 |
| - } |
| 8 | + let uri = (call.arguments as! [String: Any])["path"] as! String |
| 9 | + let localeId = (call.arguments as! [String: Any])["locale"] as! String |
| 10 | + let url = URL(fileURLWithPath: uri) |
| 11 | + guard let myRecognizer = SFSpeechRecognizer(locale: Locale.init(identifier: localeId)) else { |
| 12 | + // A recognizer is not supported for the current locale |
| 13 | + result(FlutterError(code: "FAILED_REC", message: "unsupported locale", details: nil)) |
| 14 | + return |
| 15 | + } |
| 16 | + |
| 17 | + if !myRecognizer.isAvailable { |
| 18 | + result(FlutterError(code: "FAILED_REC", message: "unavailable recognizer", details: nil)) |
| 19 | + return |
| 20 | + } |
20 | 21 |
|
21 |
| - let request = SFSpeechURLRecognitionRequest(url: url) |
22 |
| - myRecognizer.recognitionTask(with: request) { (res, error) in |
23 |
| - guard let res = res else { |
24 |
| - // Recognition failed, so check error for details and handle it |
25 |
| - result(FlutterError(code: "FAILED_REC", message: error.debugDescription, details: nil)) |
26 |
| - return |
27 |
| - } |
| 22 | + let request = SFSpeechURLRecognitionRequest(url: url) |
| 23 | + myRecognizer.recognitionTask(with: request) { (res, error) in |
| 24 | + guard let res = res else { |
| 25 | + // Recognition failed, so check error for details and handle it |
| 26 | + result(FlutterError(code: "FAILED_REC", message: error.debugDescription, details: nil)) |
| 27 | + return |
| 28 | + } |
28 | 29 |
|
29 |
| - // Print the speech that has been recognized so far |
30 |
| - if res.isFinal { |
31 |
| - result(String(res.bestTranscription.formattedString)) |
32 |
| - } |
33 |
| - } |
| 30 | + // Print the speech that has been recognized so far |
| 31 | + if res.isFinal { |
| 32 | + result(String(res.bestTranscription.formattedString)) |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + private func requestTxPermission(result: @escaping FlutterResult) { |
| 37 | + let status = SFSpeechRecognizer.authorizationStatus() |
| 38 | + switch status { |
| 39 | + case .notDetermined: |
| 40 | + SFSpeechRecognizer.requestAuthorization({ (status) -> Void in |
| 41 | + result(Bool(status == SFSpeechRecognizerAuthorizationStatus.authorized)) |
| 42 | + }) |
| 43 | + case .denied: |
| 44 | + result(Bool(false)) |
| 45 | + case .restricted: |
| 46 | + result(Bool(false)) |
| 47 | + case .authorized: |
| 48 | + result(Bool(true)) |
| 49 | + default: |
| 50 | + result(Bool(true)) |
| 51 | + } |
34 | 52 | }
|
35 |
| - private func requestTxPermission(result: @escaping FlutterResult) { |
36 |
| - let status = SFSpeechRecognizer.authorizationStatus() |
37 |
| - switch status { |
38 |
| - case .notDetermined: |
39 |
| - SFSpeechRecognizer.requestAuthorization({(status)->Void in |
40 |
| - result(Bool(status == SFSpeechRecognizerAuthorizationStatus.authorized)) |
41 |
| - }) |
42 |
| - case .denied: |
43 |
| - result(Bool(false)) |
44 |
| - case .restricted: |
45 |
| - result(Bool(false)) |
46 |
| - case .authorized: |
47 |
| - result(Bool(true)) |
48 |
| - default: |
49 |
| - result(Bool(true)) |
50 |
| - } |
| 53 | + private func getLocaleOptions(result: @escaping FlutterResult) { |
| 54 | + let locales = SFSpeechRecognizer.supportedLocales() |
| 55 | + var localeOptions: [String: String] = [:] |
| 56 | + for locale in locales { |
| 57 | + localeOptions[locale.identifier] = locale.localizedString(forIdentifier: locale.identifier)! |
51 | 58 | }
|
| 59 | + result(localeOptions) |
| 60 | + } |
52 | 61 | override func application(
|
53 | 62 | _ application: UIApplication,
|
54 | 63 | didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
|
55 | 64 | ) -> Bool {
|
56 |
| - let controller : FlutterViewController = window?.rootViewController as! FlutterViewController |
57 |
| - let transcribeChannel = FlutterMethodChannel(name: "voiceoutliner.saga.chat/iostx", binaryMessenger: controller.binaryMessenger) |
58 |
| - transcribeChannel.setMethodCallHandler({(call: FlutterMethodCall, result: @escaping FlutterResult)-> Void in |
59 |
| - switch call.method { |
60 |
| - case "transcribe": |
61 |
| - self.receiveTxRequest(call: call, result: result) |
62 |
| - case "requestPermission": |
63 |
| - self.requestTxPermission(result: result) |
64 |
| - default: |
65 |
| - result(FlutterMethodNotImplemented) |
66 |
| - } |
67 |
| - }) |
68 |
| - GeneratedPluginRegistrant.register(with: self) |
69 |
| - return super.application(application, didFinishLaunchingWithOptions: launchOptions) |
| 65 | + let controller: FlutterViewController = window?.rootViewController as! FlutterViewController |
| 66 | + let transcribeChannel = FlutterMethodChannel( |
| 67 | + name: "voiceoutliner.saga.chat/iostx", binaryMessenger: controller.binaryMessenger) |
| 68 | + transcribeChannel.setMethodCallHandler({ |
| 69 | + (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in |
| 70 | + switch call.method { |
| 71 | + case "transcribe": |
| 72 | + self.receiveTxRequest(call: call, result: result) |
| 73 | + case "requestPermission": |
| 74 | + self.requestTxPermission(result: result) |
| 75 | + case "getLocaleOptions": |
| 76 | + self.getLocaleOptions(result: result) |
| 77 | + default: |
| 78 | + result(FlutterMethodNotImplemented) |
| 79 | + } |
| 80 | + }) |
| 81 | + GeneratedPluginRegistrant.register(with: self) |
| 82 | + return super.application(application, didFinishLaunchingWithOptions: launchOptions) |
70 | 83 | }
|
71 | 84 | }
|
0 commit comments