forked from jsierles/react-native-audio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudioRecorderManager.m
182 lines (145 loc) · 4.53 KB
/
AudioRecorderManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// AudioRecorderManager.m
// AudioRecorderManager
//
// Created by Joshua Sierles on 15/04/15.
// Copyright (c) 2015 Joshua Sierles. All rights reserved.
//
#import "AudioRecorderManager.h"
#import "RCTConvert.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
#import <AVFoundation/AVFoundation.h>
NSString *const AudioRecorderEventProgress = @"recordingProgress";
NSString *const AudioRecorderEventFinished = @"recordingFinished";
@implementation AudioRecorderManager {
AVAudioRecorder *_audioRecorder;
AVAudioPlayer *_audioPlayer;
NSTimeInterval _currentTime;
id _progressUpdateTimer;
int _progressUpdateInterval;
NSDate *_prevProgressUpdateTime;
NSURL *_audioFileURL;
AVAudioSession *_recordSession;
}
@synthesize bridge = _bridge;
RCT_EXPORT_MODULE();
- (void)sendProgressUpdate {
if (_audioRecorder && _audioRecorder.recording) {
_currentTime = _audioRecorder.currentTime;
} else if (_audioPlayer && _audioPlayer.playing) {
_currentTime = _audioPlayer.currentTime;
} else {
return;
}
NSString *time = [NSString stringWithFormat:@"%f", _currentTime];
if (_prevProgressUpdateTime == nil ||
(([_prevProgressUpdateTime timeIntervalSinceNow] * -1000.0) >= _progressUpdateInterval)) {
[_bridge.eventDispatcher sendDeviceEventWithName:AudioRecorderEventProgress body:@{
@"currentTime": [NSNumber numberWithFloat:_currentTime]
}];
_prevProgressUpdateTime = [NSDate date];
}
}
- (void)stopProgressTimer {
[_progressUpdateTimer invalidate];
}
- (void)startProgressTimer {
_progressUpdateInterval = 250;
_prevProgressUpdateTime = nil;
[self stopProgressTimer];
_progressUpdateTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(sendProgressUpdate)];
[_progressUpdateTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag {
NSLog(flag ? @"FINISHED OK" : @"FINISH ERROR");
[_bridge.eventDispatcher sendDeviceEventWithName:AudioRecorderEventFinished body:@{
@"finished": @"test"
}];
}
RCT_EXPORT_METHOD(prepareRecordingAtPath:(NSString *)path)
{
_prevProgressUpdateTime = nil;
[self stopProgressTimer];
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
NSString *audioFilePath = [resourcePath stringByAppendingPathComponent:path];
_audioFileURL = [NSURL fileURLWithPath:audioFilePath];
NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:AVAudioQualityHigh], AVEncoderAudioQualityKey,
[NSNumber numberWithInt:16], AVEncoderBitRateKey,
[NSNumber numberWithInt: 2], AVNumberOfChannelsKey,
[NSNumber numberWithFloat:44100.0], AVSampleRateKey,
nil];
NSError *error = nil;
_recordSession = [AVAudioSession sharedInstance];
[_recordSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
_audioRecorder = [[AVAudioRecorder alloc]
initWithURL:_audioFileURL
settings:recordSettings
error:&error];
_audioRecorder.delegate = self;
if (error) {
NSLog(@"error: %@", [error localizedDescription]);
// TODO: dispatch error over the bridge
} else {
[_audioRecorder prepareToRecord];
}
}
RCT_EXPORT_METHOD(startRecording)
{
if (!_audioRecorder.recording) {
[self startProgressTimer];
[_recordSession setActive:YES error:nil];
[_audioRecorder record];
}
}
RCT_EXPORT_METHOD(stopRecording)
{
if (_audioRecorder.recording) {
[_audioRecorder stop];
[_recordSession setActive:NO error:nil];
_prevProgressUpdateTime = nil;
}
}
RCT_EXPORT_METHOD(pauseRecording)
{
if (_audioRecorder.recording) {
[self stopProgressTimer];
[_audioRecorder pause];
}
}
RCT_EXPORT_METHOD(playRecording)
{
if (_audioRecorder.recording) {
NSLog(@"stop the recording before playing");
return;
} else {
NSError *error;
if (!_audioPlayer.playing) {
_audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:_audioRecorder.url
error:&error];
if (error) {
[self stopProgressTimer];
NSLog(@"audio playback loading error: %@", [error localizedDescription]);
// TODO: dispatch error over the bridge
} else {
[self startProgressTimer];
[_audioPlayer play];
}
}
}
}
RCT_EXPORT_METHOD(pausePlaying)
{
if (_audioPlayer.playing) {
[_audioPlayer pause];
}
}
RCT_EXPORT_METHOD(stopPlaying)
{
if (_audioPlayer.playing) {
[_audioPlayer stop];
}
}
@end