通常録音
接続中のデバイスに、通常の音声録音セッションの開始または停止を要求します。
前提条件
- デバイスが接続済みで、操作可能な状態であること。
- デバイスが
DeviceAudioRecordingAPIに準拠していること。 - デバイスに録音用の十分な空き容量があること。
API リファレンス
フレームワーク
AIBuds.xcframework
インポート
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>プロトコル
これらの要求は DeviceAudioRecordingAPI で定義されています。
- Swift
- Objective-C
/// The protocol for device API that supports audio recording.
protocol DeviceAudioRecordingAPI: DeviceAPI {
/// Requests the device to start an audio recording session.
/// - Parameters:
/// - completion: A closure that is called when the operation completes.
/// - success: `true` if the operation was successful; otherwise `false`.
/// - statusCode: The status code returned by the device. `nil` if the operation failed.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
func requestStartAudioRecording(_ completion: AIBudsStatusCodeCompletionHandler?)
/// Requests the device to stop the current audio recording session.
/// - Parameters:
/// - completion: A closure that is called when the operation completes.
/// - success: `true` if the operation was successful; otherwise `false`.
/// - statusCode: The status code returned by the device. `nil` if the operation failed.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
func requestStopAudioRecording(_ completion: AIBudsStatusCodeCompletionHandler?)
}/// The protocol for device API that supports audio recording.
@protocol AIBudsDeviceAudioRecordingAPI <AIBudsDeviceAPI>
/// Requests the device to start an audio recording session.
/// - Parameters:
/// - completion: A closure that is called when the operation completes.
/// - success: `true` if the operation was successful; otherwise `false`.
/// - statusCode: The status code returned by the device. `nil` if the operation failed.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the
/// operation was successful.
- (void)requestStartAudioRecordingWithCompletion:
(AIBudsStatusCodeCompletionHandler _Nullable)completion;
/// Requests the device to stop the current audio recording session.
/// - Parameters:
/// - completion: A closure that is called when the operation completes.
/// - success: `true` if the operation was successful; otherwise `false`.
/// - statusCode: The status code returned by the device. `nil` if the operation failed.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the
/// operation was successful.
- (void)requestStopAudioRecordingWithCompletion:
(AIBudsStatusCodeCompletionHandler _Nullable)completion;
@endインスタンスメソッド
| メソッド | 用途 |
|---|---|
requestStartAudioRecording | デバイスに録音開始を要求します。 |
requestStopAudioRecording | デバイスに録音停止を要求します。 |
完了時の値
| 名前 | 型 | 説明 |
|---|---|---|
success | Bool / BOOL | 要求が成功したかどうか。 |
statusCode | NSNumber? | デバイスが返したステータスコード。処理失敗時は nil。 |
error | NSError? | 失敗時の詳細。成功時は nil。 |
これらのメソッドは録音ファイルのパスを返しません。結果として返されるのはデバイスコマンドの状態です。
使用例
- Swift
- Objective-C
import AIBuds
guard let device = device as? DeviceAudioRecordingAPI else {
print("Device does not support audio recording")
return
}
device.requestStartAudioRecording { success, statusCode, error in
guard success else {
print("Start failed: \(error?.localizedDescription ?? "Unknown error")")
return
}
print("Recording started, status: \(statusCode?.stringValue ?? "Unavailable")")
}
// Call this from the UI action that stops the active recording.
device.requestStopAudioRecording { success, statusCode, error in
guard success else {
print("Stop failed: \(error?.localizedDescription ?? "Unknown error")")
return
}
print("Recording stopped, status: \(statusCode?.stringValue ?? "Unavailable")")
}#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>
id<AIBudsDeviceAudioRecordingAPI> device = (id<AIBudsDeviceAudioRecordingAPI>)self.device;
if (![device conformsToProtocol:@protocol(AIBudsDeviceAudioRecordingAPI)]) {
NSLog(@"Device does not support audio recording");
return;
}
[device requestStartAudioRecordingWithCompletion:^(
BOOL success, NSNumber *_Nullable statusCode, NSError *_Nullable error) {
if (!success) {
NSLog(@"Start failed: %@", error.localizedDescription);
return;
}
NSLog(@"Recording started, status: %@", statusCode);
}];
// Call this from the UI action that stops the active recording.
[device requestStopAudioRecordingWithCompletion:^(
BOOL success, NSNumber *_Nullable statusCode, NSError *_Nullable error) {
if (!success) {
NSLog(@"Stop failed: %@", error.localizedDescription);
return;
}
NSLog(@"Recording stopped, status: %@", statusCode);
}];エラー処理
主な判定には success を使用します。デバイス固有の処理に備えて statusCode を保持し、error がある場合は内容を提示してください。公開プロトコルでは、個々のステータスコードの意味は定義されていません。