From 1c8efab1cd81fb22e9c03bb48176a8830e66c356 Mon Sep 17 00:00:00 2001 From: jaybo Date: Thu, 10 Dec 2020 11:31:13 +0800 Subject: [PATCH] fix: #46 --- ios/RNAudioRecord.m | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ios/RNAudioRecord.m b/ios/RNAudioRecord.m index 7304653..5d79a3a 100644 --- a/ios/RNAudioRecord.m +++ b/ios/RNAudioRecord.m @@ -1,5 +1,10 @@ #import "RNAudioRecord.h" +@interface RNAudioRecord () +@property (nonatomic, nullable, copy) AVAudioSessionCategory previousCategory; +@end + + @implementation RNAudioRecord RCT_EXPORT_MODULE(); @@ -28,6 +33,8 @@ @implementation RNAudioRecord RCT_EXPORT_METHOD(start) { RCTLogInfo(@"start"); + [self savePreviousCategory]; + // most audio players set session category to "Playback", record won't work in this mode // therefore set session category to "Record" before recording [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil]; @@ -56,6 +63,7 @@ @implementation RNAudioRecord AudioQueueDispose(_recordState.mQueue, true); AudioFileClose(_recordState.mAudioFile); } + [self restorePreviousCategory]; resolve(_filePath); unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:_filePath error:nil] fileSize]; RCTLogInfo(@"file path %@", _filePath); @@ -103,4 +111,15 @@ - (void)dealloc { AudioQueueDispose(_recordState.mQueue, true); } +- (void)savePreviousCategory { + self.previousCategory = [AVAudioSession sharedInstance].category; +} + +- (void)restorePreviousCategory { + if (self.previousCategory) { + [[AVAudioSession sharedInstance] setCategory:self.previousCategory error:nil]; + self.previousCategory = nil; + } +} + @end