Skip to content
This repository was archived by the owner on Aug 21, 2020. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 56 additions & 20 deletions ios/AudioRecorderManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

NSString *const AudioRecorderEventProgress = @"recordingProgress";
NSString *const AudioRecorderEventFinished = @"recordingFinished";
NSString *const AudioRecorderEventInterruptionBegin = @"recordingInterruptionBegin";
NSString *const AudioRecorderEventInterruptionEnd = @"recordingInterruptionEnd";

@implementation AudioRecorderManager {

Expand All @@ -33,16 +35,13 @@ @implementation AudioRecorderManager {
BOOL _meteringEnabled;
BOOL _measurementMode;
BOOL _includeBase64;
BOOL _resumeOnInterruptionEnd;
}

@synthesize bridge = _bridge;

RCT_EXPORT_MODULE();

+ (BOOL)requiresMainQueueSetup {
return YES;
}

- (void)sendProgressUpdate {
if (_audioRecorder && _audioRecorder.isRecording) {
_currentTime = _audioRecorder.currentTime;
Expand Down Expand Up @@ -125,31 +124,59 @@ - (NSString *) applicationDocumentsDirectory
return basePath;
}

RCT_EXPORT_METHOD(prepareRecordingAtPath:(NSString *)path sampleRate:(float)sampleRate channels:(nonnull NSNumber *)channels quality:(NSString *)quality encoding:(NSString *)encoding meteringEnabled:(BOOL)meteringEnabled measurementMode:(BOOL)measurementMode includeBase64:(BOOL)includeBase64)
-(void)audioSessionInterruptionNotification:(NSNotification*)notification {

// Check the type of notification
if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
// Check to see if it was a begin interruption
if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) {
// dispatch event over the bridge
[self.bridge.eventDispatcher sendAppEventWithName:AudioRecorderEventInterruptionBegin body:@{}];

if (_resumeOnInterruptionEnd) {
[self pauseRecording];
} else { // stop recording
_audioRecorder.delegate = nil; // so that audioRecorderDidFinishRecording may not get called on stopRecording
[self stopRecording];
NSError *error;

// inactivate session
[_recordSession setActive:NO error:&error];

if (error) {
NSLog(@"%@", error);
}
}

} else if([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeEnded]]){
// dispatch event over the bridge
[self.bridge.eventDispatcher sendAppEventWithName:AudioRecorderEventInterruptionEnd body:@{}];

if (_resumeOnInterruptionEnd) {
[self resumeRecording];
}
}
}
}

RCT_EXPORT_METHOD(prepareRecordingAtPath:(NSString *)path sampleRate:(float)sampleRate channels:(nonnull NSNumber *)channels quality:(NSString *)quality encoding:(NSString *)encoding meteringEnabled:(BOOL)meteringEnabled measurementMode:(BOOL)measurementMode includeBase64:(BOOL)includeBase64 resumeOnInterruptionEnd:(BOOL)resumeOnInterruptionEnd)
{
// Allow to execute actions when the app is not in foreground
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

_prevProgressUpdateTime = nil;
[self stopProgressTimer];

NSString *filePathAndDirectory = [path stringByDeletingLastPathComponent];
NSError *error=nil;
//create parent dirs if necessary
if (![[NSFileManager defaultManager] createDirectoryAtPath:filePathAndDirectory
withIntermediateDirectories:YES
attributes:nil
error:&error])
{
NSLog(@"Create directory error: %@", error);
}


_audioFileURL = [NSURL fileURLWithPath:path];

// Default options
_audioQuality = [NSNumber numberWithInt:AVAudioQualityHigh];
_audioEncoding = [NSNumber numberWithInt:kAudioFormatAppleIMA4];
_audioChannels = [NSNumber numberWithInt:2];
_audioSampleRate = [NSNumber numberWithFloat:44100.0];
_meteringEnabled = NO;
_includeBase64 = NO;
_resumeOnInterruptionEnd = YES;

// Set audio quality from options
if (quality != nil) {
Expand Down Expand Up @@ -223,8 +250,17 @@ - (NSString *) applicationDocumentsDirectory
_includeBase64 = includeBase64;
}

if (resumeOnInterruptionEnd != nil && resumeOnInterruptionEnd != YES) {
_resumeOnInterruptionEnd = resumeOnInterruptionEnd;
}

NSError *error = nil;

_recordSession = [AVAudioSession sharedInstance];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(audioSessionInterruptionNotification:)
name:AVAudioSessionInterruptionNotification
object:_recordSession];

if (_measurementMode) {
[_recordSession setCategory:AVAudioSessionCategoryRecord error:nil];
Expand All @@ -244,8 +280,8 @@ - (NSString *) applicationDocumentsDirectory
if (error) {
NSLog(@"error: %@", [error localizedDescription]);
// TODO: dispatch error over the bridge
} else {
[_audioRecorder prepareToRecord];
} else {
BOOL prepared = [_audioRecorder prepareToRecord];
}
}

Expand Down