|
1 | 1 | #import "RNAudioRecord.h" |
2 | 2 |
|
| 3 | +@interface RNAudioRecord () |
| 4 | +@property (nonatomic, nullable, copy) AVAudioSessionCategory previousCategory; |
| 5 | +@end |
| 6 | + |
| 7 | + |
3 | 8 | @implementation RNAudioRecord |
4 | 9 |
|
5 | 10 | RCT_EXPORT_MODULE(); |
@@ -28,6 +33,8 @@ @implementation RNAudioRecord |
28 | 33 | RCT_EXPORT_METHOD(start) { |
29 | 34 | RCTLogInfo(@"start"); |
30 | 35 |
|
| 36 | + [self savePreviousCategory]; |
| 37 | + |
31 | 38 | // most audio players set session category to "Playback", record won't work in this mode |
32 | 39 | // therefore set session category to "Record" before recording |
33 | 40 | [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil]; |
@@ -56,6 +63,7 @@ @implementation RNAudioRecord |
56 | 63 | AudioQueueDispose(_recordState.mQueue, true); |
57 | 64 | AudioFileClose(_recordState.mAudioFile); |
58 | 65 | } |
| 66 | + [self restorePreviousCategory]; |
59 | 67 | resolve(_filePath); |
60 | 68 | unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:_filePath error:nil] fileSize]; |
61 | 69 | RCTLogInfo(@"file path %@", _filePath); |
@@ -103,4 +111,15 @@ - (void)dealloc { |
103 | 111 | AudioQueueDispose(_recordState.mQueue, true); |
104 | 112 | } |
105 | 113 |
|
| 114 | +- (void)savePreviousCategory { |
| 115 | + self.previousCategory = [AVAudioSession sharedInstance].category; |
| 116 | +} |
| 117 | + |
| 118 | +- (void)restorePreviousCategory { |
| 119 | + if (self.previousCategory) { |
| 120 | + [[AVAudioSession sharedInstance] setCategory:self.previousCategory error:nil]; |
| 121 | + self.previousCategory = nil; |
| 122 | + } |
| 123 | +} |
| 124 | + |
106 | 125 | @end |
0 commit comments