1
This commit is contained in:
@@ -175,16 +175,18 @@ self.voiceInputBar.enabled = YES;
|
||||
|
||||
---
|
||||
|
||||
## 🎯 完整示例(集成 Deepgram)
|
||||
## 🎯 完整示例(桥接录音事件)
|
||||
|
||||
```objc
|
||||
#import "YourViewController.h"
|
||||
#import "KBVoiceInputBar.h"
|
||||
#import "DeepgramStreamingManager.h"
|
||||
#import "KBVoiceToTextManager.h"
|
||||
#import "KBVoiceRecordManager.h"
|
||||
|
||||
@interface YourViewController () <KBVoiceInputBarDelegate, DeepgramStreamingManagerDelegate>
|
||||
@interface YourViewController () <KBVoiceToTextManagerDelegate, KBVoiceRecordManagerDelegate>
|
||||
@property (nonatomic, strong) KBVoiceInputBar *voiceInputBar;
|
||||
@property (nonatomic, strong) DeepgramStreamingManager *deepgramManager;
|
||||
@property (nonatomic, strong) KBVoiceToTextManager *voiceToTextManager;
|
||||
@property (nonatomic, strong) KBVoiceRecordManager *voiceRecordManager;
|
||||
@end
|
||||
|
||||
@implementation YourViewController
|
||||
@@ -192,7 +194,7 @@ self.voiceInputBar.enabled = YES;
|
||||
- (void)viewDidLoad {
|
||||
[super viewDidLoad];
|
||||
[self setupUI];
|
||||
[self setupDeepgram];
|
||||
[self setupVoiceManagers];
|
||||
}
|
||||
|
||||
- (void)setupUI {
|
||||
@@ -205,50 +207,44 @@ self.voiceInputBar.enabled = YES;
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)setupDeepgram {
|
||||
self.deepgramManager = [[DeepgramStreamingManager alloc] init];
|
||||
self.deepgramManager.delegate = self;
|
||||
self.deepgramManager.serverURL = @"wss://api.deepgram.com/v1/listen";
|
||||
self.deepgramManager.apiKey = @"your_api_key";
|
||||
[self.deepgramManager prepareConnection];
|
||||
- (void)setupVoiceManagers {
|
||||
self.voiceToTextManager = [[KBVoiceToTextManager alloc] initWithInputBar:self.voiceInputBar];
|
||||
self.voiceToTextManager.delegate = self;
|
||||
|
||||
self.voiceRecordManager = [[KBVoiceRecordManager alloc] init];
|
||||
self.voiceRecordManager.delegate = self;
|
||||
}
|
||||
|
||||
#pragma mark - KBVoiceInputBarDelegate
|
||||
#pragma mark - KBVoiceToTextManagerDelegate
|
||||
|
||||
- (void)voiceInputBarDidBeginRecording:(KBVoiceInputBar *)inputBar {
|
||||
inputBar.statusText = @"正在连接...";
|
||||
[self.deepgramManager start];
|
||||
- (void)voiceToTextManagerDidBeginRecording:(KBVoiceToTextManager *)manager {
|
||||
[self.voiceRecordManager startRecording];
|
||||
}
|
||||
|
||||
- (void)voiceInputBarDidEndRecording:(KBVoiceInputBar *)inputBar {
|
||||
inputBar.statusText = @"正在识别...";
|
||||
[self.deepgramManager stopAndFinalize];
|
||||
- (void)voiceToTextManagerDidEndRecording:(KBVoiceToTextManager *)manager {
|
||||
[self.voiceRecordManager stopRecording];
|
||||
}
|
||||
|
||||
- (void)voiceInputBarDidCancelRecording:(KBVoiceInputBar *)inputBar {
|
||||
inputBar.statusText = @"已取消";
|
||||
[self.deepgramManager cancel];
|
||||
- (void)voiceToTextManagerDidCancelRecording:(KBVoiceToTextManager *)manager {
|
||||
[self.voiceRecordManager cancelRecording];
|
||||
}
|
||||
|
||||
#pragma mark - DeepgramStreamingManagerDelegate
|
||||
#pragma mark - KBVoiceRecordManagerDelegate
|
||||
|
||||
- (void)deepgramStreamingManagerDidConnect {
|
||||
self.voiceInputBar.statusText = @"正在聆听...";
|
||||
- (void)voiceRecordManager:(KBVoiceRecordManager *)manager
|
||||
didFinishRecordingAtURL:(NSURL *)fileURL
|
||||
duration:(NSTimeInterval)duration {
|
||||
NSLog(@"录音完成:%@ %.2fs", fileURL, duration);
|
||||
// TODO: 上传音频文件并处理转写结果
|
||||
}
|
||||
|
||||
- (void)deepgramStreamingManagerDidUpdateRMS:(float)rms {
|
||||
[self.voiceInputBar updateVolumeRMS:rms];
|
||||
- (void)voiceRecordManagerDidRecordTooShort:(KBVoiceRecordManager *)manager {
|
||||
NSLog(@"录音过短");
|
||||
}
|
||||
|
||||
- (void)deepgramStreamingManagerDidReceiveInterimTranscript:(NSString *)text {
|
||||
self.voiceInputBar.statusText = text.length > 0 ? text : @"正在识别...";
|
||||
}
|
||||
|
||||
- (void)deepgramStreamingManagerDidReceiveFinalTranscript:(NSString *)text {
|
||||
self.voiceInputBar.statusText = @"识别完成";
|
||||
NSLog(@"最终识别结果:%@", text);
|
||||
|
||||
// TODO: 处理识别结果
|
||||
- (void)voiceRecordManager:(KBVoiceRecordManager *)manager
|
||||
didFailWithError:(NSError *)error {
|
||||
NSLog(@"录音失败:%@", error.localizedDescription);
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -316,5 +312,6 @@ _recordButton.tintColor = [UIColor systemBlueColor];
|
||||
## 🔗 相关组件
|
||||
|
||||
- `KBAiRecordButton`:录音按钮(支持长按、波形动画)
|
||||
- `DeepgramStreamingManager`:语音识别管理器
|
||||
- `KBVoiceToTextManager`:语音输入事件桥接层
|
||||
- `KBVoiceRecordManager`:录音文件管理器
|
||||
- `VoiceChatStreamingManager`:语音聊天管理器
|
||||
|
||||
Reference in New Issue
Block a user