Skip to content

Commit f7cf732

Browse files
authored
fix: fixed issue with drums example while maintaining recorder example still working (#607)
1 parent 1211ecb commit f7cf732

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

packages/react-native-audio-api/ios/audioapi/ios/core/NativeAudioPlayer.m

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,20 @@ - (instancetype)initWithRenderAudio:(RenderAudioBlock)renderAudio
3535
- (bool)start
3636
{
3737
NSLog(@"[AudioPlayer] start");
38-
38+
3939
AudioEngine *audioEngine = [AudioEngine sharedInstance];
4040
assert(audioEngine != nil);
41-
self.sourceNodeId = [audioEngine attachSourceNode:self.sourceNode format:self.format];
42-
41+
4342
// AudioEngine allows us to attach and connect nodes at runtime but with few limitations
4443
// in this case if it is the first player and recorder started the engine we need to restart.
4544
// It can be optimized by tracking if we haven't break rules of at runtime modifications from docs
4645
// https://developer.apple.com/documentation/avfaudio/avaudioengine?language=objc
4746
//
4847
// Currently we are restarting because we do not see any significant performance issue and case when
4948
// you will need to start and stop player very frequently
50-
return [audioEngine restartAudioEngine];
49+
[audioEngine stopEngine];
50+
self.sourceNodeId = [audioEngine attachSourceNode:self.sourceNode format:self.format];
51+
return [audioEngine startIfNecessary];
5152
}
5253

5354
- (void)stop
@@ -57,7 +58,6 @@ - (void)stop
5758
AudioEngine *audioEngine = [AudioEngine sharedInstance];
5859
assert(audioEngine != nil);
5960
[audioEngine detachSourceNodeWithId:self.sourceNodeId];
60-
[audioEngine restartAudioEngine];
6161
[audioEngine stopIfNecessary];
6262
self.sourceNodeId = nil;
6363
}
@@ -67,9 +67,8 @@ - (bool)resume
6767
NSLog(@"[AudioPlayer] resume");
6868
AudioEngine *audioEngine = [AudioEngine sharedInstance];
6969
assert(audioEngine != nil);
70-
[audioEngine startEngine];
7170

72-
return [audioEngine startEngine];
71+
return [audioEngine startIfNecessary];
7372
}
7473

7574
- (void)suspend

packages/react-native-audio-api/ios/audioapi/ios/core/NativeAudioRecorder.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ - (void)start
100100
{
101101
AudioEngine *audioEngine = [AudioEngine sharedInstance];
102102
assert(audioEngine != nil);
103-
[audioEngine attachInputNode:self.sinkNode];
104103

105104
// AudioEngine allows us to attach and connect nodes at runtime but with few limitations
106105
// in this case if it is the first recorder node and player started the engine we need to restart.
@@ -109,15 +108,16 @@ - (void)start
109108
//
110109
// Currently we are restarting because we do not see any significant performance issue and case when
111110
// you will need to start and stop recorder very frequently
112-
[audioEngine restartAudioEngine];
111+
[audioEngine stopEngine];
112+
[audioEngine attachInputNode:self.sinkNode];
113+
[audioEngine startIfNecessary];
113114
}
114115

115116
- (void)stop
116117
{
117118
AudioEngine *audioEngine = [AudioEngine sharedInstance];
118119
assert(audioEngine != nil);
119120
[audioEngine detachInputNode];
120-
[audioEngine restartAudioEngine];
121121
[audioEngine stopIfNecessary];
122122
}
123123

0 commit comments

Comments
 (0)