구현
인식 시작하기
먼저, SORI 프레임워크를 사용할 곳에 임포트해야 합니다.
swift
import SORI
SORIManager
클래스의 start
메서드로 간단하게 시작할 수 있습니다. 이 메서드는 내장 오디오 레코더와 함께 작동합니다. 다음은 예시입니다:
swift
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)
})
)
녹음된 오디오는 어디에도 전송되지 않으므로, 개인 정보 보호에 대한 우려가 전혀 없습니다.
인식 결과 데이터 구조
다음은 응답으로 받게 될 데이터 구조의 예시입니다.
json
{
"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"
}
인식 중지하기
SORIManager
클래스의 stop
메서드로 중지할 수 있습니다. 오디오 레코더도 작동을 중지합니다.
swift
SORIManager.shared().stop()
백그라운드 인식
백그라운드 인식이 필요하신가요? 앱의 Info.plist
를 열고, UIBackgroundModes
키를 추가하고 여기에 audio
값을 가진 항목을 추가하세요. 다음은 예시입니다:
xml
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
<string>remote-notification</string>
<string>audio</string>
</array>
백그라운드 인식은 앱이 시작되고 실행 중일 때 작동합니다. 반대로, 앱이 종료되면 백그라운드 인식도 중지됩니다.
오류 처리
SORI SDK의 인식 프로세스는 모두 비 메인 스레드에서 수행되기 때문에, 이 과정에서 발생하는 모든 오류는 Notification을 통해 전달됩니다. 이 알림을 받으려면 NotificationCenter를 통해 SORIError.errorNotificationName()
의 알림을 관찰할 수 있습니다.
swift
NotificationCenter.default.addObserver(forName: SORIError.errorNotificationName(), object: nil, queue: OperationQueue.main) { notification in
// SORI 오류 처리
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는 다음과 같이 정의됩니다:
swift
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
}
objective-c
typedef NS_ENUM(NSInteger, SORIErrorCode) {
SORIErrorCodeNone = 0,
SORIErrorCodeBeginInterruption = 100,
SORIErrorCodeFinishInterruption = 101,
SORIErrorCodeNetworkNotReachable = 400,
SORIErrorCodeAuthenticationFailure = 401,
SORIErrorCodeAudioRecordDenied = 402,
SORIErrorCodeServerError = 500,
SORIErrorCodeAudioServiceFailure = 501,
SORIErrorCodeFileOperationFailure = 502,
SORIErrorCodeInvalidResponses = 503,
SORIErrorCodeFailedToDownloadAudiopacks = 504,
};