Implementation
Start recognition
First of all, you need to import SORI framework where you will use it.
import SORI
You can start simply with the start
method of class SORIManager
. This method works with an built-in audio recorder. Here is an example:
SORIManager.shared().applicationID = "application ID string"
SORIManager.shared().secretKey = "secret key string"
SORIManager.shared().apiVersion = .V3
SORIManager.shared().resultType = .local
SORIManager.shared().start(
type: .local
repeat: true,
recognitionHandler: {
[unowned self] (media, error) in
self.recognized(mediaJSON: media as? [String : Any], error: error)
})
)
Since the recorded audio is not transmitted anywhere, there is no privacy concern at all.
Recognized result data structure
Below is an example of the data structure you will receive in response.
{
"position": 3187,
"campaign": {
"_id": "campaign identifier",
"action_url": "https://foo.bar/action/url",
"created_at": "2023-01-01T00:00:48.187000+00:00",
"image": "https://foo.bar/image.jpg",
"name": "campaign name",
"operating_period": {
"start": "2023-01-01T01:01:10.610000+00:00",
"until": "2024-12-31T12:59:59.999000+00:00",
},
"state": "pending",
"title": "campaign title",
"type": "link"
},
"_id": "material identifier",
"duration": 29.375,
"type": "cf",
"image": "https://foo.bar/material/cover/image.png",
"score": -32.84499943256378,
"title": "material title"
}
Stop recognition
It can be stopped by stop
method of class SORIManager
. The audio recorder also stops working.
SORIManager.shared().stop()
Background recognition
Do you need background recognition? Open your app's Info.plist
. Add key UIBackgroundModes
and add an item with value audio
to it. Here is an example:
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>remote-notification</string>
<string>audio</string>
</array>
Background recognition works when the app start and running. Conversely, when the app is terminated, background recognition also stops.
Error handling
Since the recognition process of SORI SDK is all performed in the non-main thread, all errors occurring during this process are delivered through Notification. In order to receive this notification, you can observe notification of SORIError.errorNotificationName()
through NotificationCenter.
NotificationCenter.default.addObserver(forName: SORIError.errorNotificationName(), object: nil, queue: OperationQueue.main) { notification in
// Handling SORI error
guard let error = notification.object as? NSError, let code = SORIErrorCode(rawValue: error.code) else {
print("Unknown error notification: \(notification)")
return
}
if code == .authenticationFailure {
print("Failed to authentication")
}
}
SORIErrorCode is defined as:
enum SORIErrorCode: Int {
case .none = 0
case .beginInterruption = 100
case .finishInterruption = 101
case .networkNotReachable = 400
case .authenticationFailure = 401
case .audioRecordDenied = 402
case .serverError = 500
case .audioServiceFailure = 501
case .fileOperationFailure = 502
case .invalidResponses = 503
case .failedToDownloadAudiopacks = 504
}