处理崩溃
This commit is contained in:
@@ -32,7 +32,7 @@
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[self cancel];
|
||||
[self cancelInternal];
|
||||
}
|
||||
|
||||
#pragma mark - Public Methods
|
||||
|
||||
@@ -52,8 +52,9 @@ static NSString *const kDeepgramStreamingManagerErrorDomain =
|
||||
_audioCapture = [[AudioCaptureManager alloc] init];
|
||||
_audioCapture.delegate = self;
|
||||
|
||||
_client = [[DeepgramWebSocketClient alloc] init];
|
||||
_client.delegate = self;
|
||||
/// 不需要自己处理音频转文本,改为录音结束把文件传递给后端
|
||||
// _client = [[DeepgramWebSocketClient alloc] init];
|
||||
// _client.delegate = self;
|
||||
|
||||
_serverURL = @"wss://api.deepgram.com/v1/listen";
|
||||
_encoding = @"linear16";
|
||||
@@ -81,7 +82,7 @@ static NSString *const kDeepgramStreamingManagerErrorDomain =
|
||||
|
||||
- (void)dealloc {
|
||||
[self removeNotifications];
|
||||
[self disconnect];
|
||||
[self disconnectInternal];
|
||||
}
|
||||
|
||||
- (void)start {
|
||||
@@ -203,6 +204,11 @@ static NSString *const kDeepgramStreamingManagerErrorDomain =
|
||||
|
||||
- (void)disconnect {
|
||||
dispatch_async(self.stateQueue, ^{
|
||||
[self disconnectInternal];
|
||||
});
|
||||
}
|
||||
|
||||
- (void)disconnectInternal {
|
||||
if (self.streaming) {
|
||||
[self.audioCapture stopCapture];
|
||||
self.streaming = NO;
|
||||
@@ -215,7 +221,6 @@ static NSString *const kDeepgramStreamingManagerErrorDomain =
|
||||
[self stopKeepAlive];
|
||||
[self.client disconnect];
|
||||
[self.audioSession deactivateSession];
|
||||
});
|
||||
}
|
||||
|
||||
#pragma mark - AudioCaptureManagerDelegate
|
||||
|
||||
@@ -40,7 +40,7 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[self disconnect];
|
||||
[self disconnectInternal];
|
||||
}
|
||||
|
||||
#pragma mark - Public Methods
|
||||
@@ -165,20 +165,20 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
|
||||
[self upsertQueryItemWithName:@"model" value:self.model items:items];
|
||||
[self upsertQueryItemWithName:@"language" value:self.language items:items];
|
||||
|
||||
[self upsertQueryItemWithName:@"punctuate"
|
||||
value:(self.punctuate ? @"true" : @"false")
|
||||
items:items];
|
||||
[self
|
||||
upsertQueryItemWithName:@"punctuate"
|
||||
value:(self.punctuate ? @"true" : @"false")items:items];
|
||||
[self upsertQueryItemWithName:@"smart_format"
|
||||
value:(self.smartFormat ? @"true" : @"false")
|
||||
items:items];
|
||||
value:(self.smartFormat ? @"true" : @"false")items
|
||||
:items];
|
||||
[self upsertQueryItemWithName:@"interim_results"
|
||||
value:(self.interimResults ? @"true" : @"false")
|
||||
items:items];
|
||||
value:(self.interimResults ? @"true" : @"false")items
|
||||
:items];
|
||||
|
||||
[self upsertQueryItemWithName:@"encoding" value:self.encoding items:items];
|
||||
[self upsertQueryItemWithName:@"sample_rate"
|
||||
value:[NSString stringWithFormat:@"%.0f",
|
||||
self.sampleRate]
|
||||
value:[NSString
|
||||
stringWithFormat:@"%.0f", self.sampleRate]
|
||||
items:items];
|
||||
[self upsertQueryItemWithName:@"channels"
|
||||
value:[NSString stringWithFormat:@"%d", self.channels]
|
||||
@@ -220,8 +220,7 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
|
||||
return;
|
||||
}
|
||||
|
||||
NSString *jsonString =
|
||||
[[NSString alloc] initWithData:jsonData
|
||||
NSString *jsonString = [[NSString alloc] initWithData:jsonData
|
||||
encoding:NSUTF8StringEncoding];
|
||||
if (!jsonString) {
|
||||
[self reportErrorWithMessage:@"Failed to encode JSON message"];
|
||||
@@ -231,8 +230,7 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
|
||||
dispatch_async(self.networkQueue, ^{
|
||||
NSURLSessionWebSocketMessage *message =
|
||||
[[NSURLSessionWebSocketMessage alloc] initWithString:jsonString];
|
||||
[self.webSocketTask
|
||||
sendMessage:message
|
||||
[self.webSocketTask sendMessage:message
|
||||
completionHandler:^(NSError *_Nullable error) {
|
||||
if (error) {
|
||||
[self reportError:error];
|
||||
@@ -307,14 +305,15 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
|
||||
}
|
||||
|
||||
NSArray *alternatives = channel[@"alternatives"];
|
||||
if (![alternatives isKindOfClass:[NSArray class]] || alternatives.count == 0) {
|
||||
if (![alternatives isKindOfClass:[NSArray class]] ||
|
||||
alternatives.count == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
NSDictionary *firstAlt = alternatives.firstObject;
|
||||
NSString *transcript = firstAlt[@"transcript"] ?: @"";
|
||||
BOOL isFinal = [json[@"is_final"] boolValue] ||
|
||||
[json[@"speech_final"] boolValue];
|
||||
BOOL isFinal =
|
||||
[json[@"is_final"] boolValue] || [json[@"speech_final"] boolValue];
|
||||
|
||||
if (transcript.length == 0) {
|
||||
return;
|
||||
@@ -364,11 +363,10 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
|
||||
}
|
||||
|
||||
- (void)reportErrorWithMessage:(NSString *)message {
|
||||
NSError *error = [NSError errorWithDomain:kDeepgramWebSocketClientErrorDomain
|
||||
NSError *error =
|
||||
[NSError errorWithDomain:kDeepgramWebSocketClientErrorDomain
|
||||
code:-1
|
||||
userInfo:@{
|
||||
NSLocalizedDescriptionKey : message ?: @""
|
||||
}];
|
||||
userInfo:@{NSLocalizedDescriptionKey : message ?: @""}];
|
||||
[self reportError:error];
|
||||
}
|
||||
|
||||
@@ -376,8 +374,8 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
|
||||
self.connected = NO;
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
if ([self.delegate respondsToSelector:@selector
|
||||
(deepgramClientDidDisconnect:)]) {
|
||||
if ([self.delegate
|
||||
respondsToSelector:@selector(deepgramClientDidDisconnect:)]) {
|
||||
[self.delegate deepgramClientDidDisconnect:error];
|
||||
}
|
||||
});
|
||||
@@ -391,7 +389,8 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
|
||||
self.connected = YES;
|
||||
NSLog(@"[DeepgramWebSocketClient] Connected");
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
if ([self.delegate respondsToSelector:@selector(deepgramClientDidConnect)]) {
|
||||
if ([self.delegate
|
||||
respondsToSelector:@selector(deepgramClientDidConnect)]) {
|
||||
[self.delegate deepgramClientDidConnect];
|
||||
}
|
||||
});
|
||||
@@ -404,8 +403,7 @@ static NSString *const kDeepgramWebSocketClientErrorDomain =
|
||||
if (!self.webSocketTask) {
|
||||
return;
|
||||
}
|
||||
NSLog(@"[DeepgramWebSocketClient] Closed with code: %ld",
|
||||
(long)closeCode);
|
||||
NSLog(@"[DeepgramWebSocketClient] Closed with code: %ld", (long)closeCode);
|
||||
[self notifyDisconnect:nil];
|
||||
[self disconnectInternal];
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ static NSString *const kVoiceChatStreamingManagerErrorDomain =
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[self disconnect];
|
||||
[self disconnectInternal];
|
||||
}
|
||||
|
||||
- (void)setServerURL:(NSString *)serverURL {
|
||||
@@ -102,6 +102,11 @@ static NSString *const kVoiceChatStreamingManagerErrorDomain =
|
||||
|
||||
- (void)disconnect {
|
||||
dispatch_async(self.stateQueue, ^{
|
||||
[self disconnectInternal];
|
||||
});
|
||||
}
|
||||
|
||||
- (void)disconnectInternal {
|
||||
if (self.streaming) {
|
||||
[self.audioCapture stopCapture];
|
||||
self.streaming = NO;
|
||||
@@ -110,7 +115,6 @@ static NSString *const kVoiceChatStreamingManagerErrorDomain =
|
||||
[self.webSocketClient disconnect];
|
||||
[self.audioSession deactivateSession];
|
||||
self.sessionId = nil;
|
||||
});
|
||||
}
|
||||
|
||||
#pragma mark - Private Methods
|
||||
|
||||
@@ -35,7 +35,7 @@ static NSString *const kVoiceChatWebSocketClientErrorDomain =
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[self disconnect];
|
||||
[self disconnectInternal];
|
||||
}
|
||||
|
||||
#pragma mark - Public Methods
|
||||
|
||||
Reference in New Issue
Block a user