AI 録音
AI 録音サービスのセッションを開始し、接続中のデバイスでの録音と連携しながら、リアルタイム文字起こし、セッションイベント、最終録音レポートを受け取ります。
このコールバックモデルで使用する AIAudioRecordingEventType のライフサイクルは、AI セッションイベントを参照してください。
Animated workflow
AI 録音セッションのライフサイクル
デバイス側の音声送信より先に AI サービスを開始し、両方のライフサイクルを連携させ、サービスのレポートコールバックで終了します。
前提条件
AIBudsAISDKが初期化済みで、登録済みの AI サービスプロバイダーが選択されていること。- 必要なプロバイダー認証が完了していること。
- デバイスが接続済みで、
DeviceAudioRecordingAPIに準拠していること。 - 選択中のプロバイダーが
AIAudioRecordingServiceAPIに対応していること。
AI を活用して実装
AI で実装
AI でこのワークフローを実装
公式の「AIBuds AI 音声録音の実装」スキルを使い、アプリに合わせて実装します。
https://docs-aibuds.github.io/ja/skills/implement-aibuds-ai-audio-recording を読み、その指示に従ってください。このスキルで「AIBuds AI 音声録音の実装」をこの iOS プロジェクトに実装し、検証してください。API リファレンス
フレームワーク
AIBudsAI.xcframework
インポート
- Swift
- Objective-C
import AIBuds
import AIBudsAI
import AIBudsAIFoundation#import <AIBuds/AIBuds-Swift.h>
#import <AIBudsAI/AIBudsAI-Swift.h>宣言
- Swift
- Objective-C
/// Starts an AI audio recording session.
/// - Parameters:
/// - config: The session configuration.
/// - onStartSuccess: Called with the started session.
/// - onStartFailure: Called when the session cannot start.
/// - onTranscript: Called when speech is transcribed.
/// - onEvent: Called for session-level events.
/// - onError: Called when the running session encounters an error.
/// - onFinish: Called with the completed session report.
public static func startAIAudioRecording(_ config: AIAudioRecordingSessionConfig = .default,
onStartSuccess: ((_ session: AIAudioRecordingSessionConvertible) -> Void)? = nil,
onStartFailure: ((_ error: Error) -> Void)? = nil,
onTranscript: ((_ transcriptData: StreamSpeechASRModel) -> Void)? = nil,
onEvent: ((_ event: AIAudioRecordingEventModel) -> Void)? = nil,
onError: ((_ error: NSError) -> Void)? = nil,
onFinish: ((_ report: AIAudioRecordingReportModel) -> Void)? = nil) -> Void
/// Stops the current AI audio recording service session.
public static func stopAIAudioRecording()/// Starts an AI audio recording session.
/// - Parameters:
/// - config: The session configuration.
/// - onStartSuccess: Called with the started session.
/// - onStartFailure: Called when the session cannot start.
/// - onTranscript: Called when speech is transcribed.
/// - onEvent: Called for session-level events.
/// - onError: Called when the running session encounters an error.
/// - onFinish: Called with the completed session report.
+ (void)startAIAudioRecordingWithConfig:(AIBudsAIAudioRecordingSessionConfig * _Nonnull)config
onStartSuccess:(void (^ _Nullable)(id <AIBudsAIAudioRecordingSessionConvertible> _Nonnull))onStartSuccess
onStartFailure:(void (^ _Nullable)(NSError * _Nonnull))onStartFailure
onTranscript:(void (^ _Nullable)(AIBudsStreamSpeechASRModel * _Nonnull))onTranscript
onEvent:(void (^ _Nullable)(AIBudsAIAudioRecordingEventModel * _Nonnull))onEvent
onError:(void (^ _Nullable)(NSError * _Nonnull))onError
onFinish:(void (^ _Nullable)(AIBudsAIAudioRecordingReportModel * _Nonnull))onFinish;
/// Stops the current AI audio recording service session.
+ (void)stopAIAudioRecording;startAIAudioRecording と stopAIAudioRecording を参照してください。
設定
AIAudioRecordingSessionConfig では次の項目を設定できます。
| プロパティ | 説明 |
|---|---|
recordingScene | .onSite などの録音シーン。 |
allowRecordingWhileOffline | ネットワークを利用できない状態でもサービスを開始できるかどうか。既定値は false。 |
enableSpeakerDiarization | 話者分離を有効にするかどうか。既定値は false。 |
languageForSpeechInput | 任意の音声言語 ID。省略時はアプリの表示言語が使われます。 |
使用例
デバイスが AI 録音の音声を送信する前に、AI サービスのセッションを開始します。ユーザーが録音を終了したとき、またはエラーが発生したときは、両方を停止してください。
- Swift
- Objective-C
let config = AIAudioRecordingSessionConfig(
recordingScene: .onSite,
allowRecordingWhileOffline: true,
enableSpeakerDiarization: true,
languageForSpeechInput: "en-US"
)
AIBudsAISDK.startAIAudioRecording(
config,
onStartSuccess: { session in
currentSession = session
guard let recordingDevice = device as? DeviceAudioRecordingAPI else {
AIBudsAISDK.stopAIAudioRecording()
return
}
recordingDevice.startAIAudioRecording(.onSite) { success, error in
if !success {
print(error?.localizedDescription ?? "Device recording failed")
AIBudsAISDK.stopAIAudioRecording()
}
}
},
onStartFailure: { error in
print("Session start failed: \(error)")
},
onTranscript: { transcript in
print(transcript.transcript ?? "")
},
onEvent: { event in
print(event)
},
onError: { error in
print(error.localizedDescription)
},
onFinish: { report in
currentSession = nil
print(report)
}
)AIBudsAIAudioRecordingSessionConfig *config =
[[AIBudsAIAudioRecordingSessionConfig alloc] initWithRecordingScene:AIBudsRecordingSceneOnSite
allowRecordingWhileOffline:YES
enableSpeakerDiarization:YES
languageForSpeechInput:@"en-US"];
[AIBudsAISDK startAIAudioRecordingWithConfig:config
onStartSuccess:^(id<AIBudsAIAudioRecordingSessionConvertible> session) {
self.currentSession = session;
id<AIBudsDeviceAudioRecordingAPI> recordingDevice =
(id<AIBudsDeviceAudioRecordingAPI>)self.device;
if (![recordingDevice conformsToProtocol:@protocol(AIBudsDeviceAudioRecordingAPI)]) {
[AIBudsAISDK stopAIAudioRecording];
return;
}
[recordingDevice startAIAudioRecordingWithScene:AIBudsRecordingSceneOnSite
completion:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"%@",
error.localizedDescription
?: @"Device recording failed");
[AIBudsAISDK stopAIAudioRecording];
}
}];
}
onStartFailure:^(NSError *error) {
NSLog(@"Session start failed: %@", error);
}
onTranscript:^(AIBudsStreamSpeechASRModel *transcript) {
NSLog(@"%@", transcript.transcript ?: @"");
}
onEvent:^(AIBudsAIAudioRecordingEventModel *event) {
NSLog(@"%@", event);
}
onError:^(NSError *error) {
NSLog(@"%@", error.localizedDescription);
}
onFinish:^(AIBudsAIAudioRecordingReportModel *report) {
self.currentSession = nil;
NSLog(@"%@", report);
}];停止時は、接続中のデバイスで stopAIAudioRecording(_:completion:) を呼び出し、保持しているセッションを破棄してから AIBudsAISDK.stopAIAudioRecording() を呼び出します。
注意事項
- 最終コールバックが返すのは
AIAudioRecordingReportModelであり、ローカル録音ファイルのパスではありません。 onStartSuccessが返すセッションを処理終了まで保持してください。- 開始時の失敗は
onStartFailure、開始後のエラーはonErrorで通知されます。 - この AI サービスには
isRecording()メソッドがありません。保持中のセッションをアプリの状態として管理してください。