Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jaybowang committed Dec 10, 2020
1 parent 6dd4c71 commit 1c8efab
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions ios/RNAudioRecord.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#import "RNAudioRecord.h"

@interface RNAudioRecord ()
@property (nonatomic, nullable, copy) AVAudioSessionCategory previousCategory;
@end


@implementation RNAudioRecord

RCT_EXPORT_MODULE();
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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

0 comments on commit 1c8efab

Please sign in to comment.