@@ -35,19 +35,20 @@ - (instancetype)initWithRenderAudio:(RenderAudioBlock)renderAudio
35
35
- (bool )start
36
36
{
37
37
NSLog (@" [AudioPlayer] start" );
38
-
38
+
39
39
AudioEngine *audioEngine = [AudioEngine sharedInstance ];
40
40
assert (audioEngine != nil );
41
- self.sourceNodeId = [audioEngine attachSourceNode: self .sourceNode format: self .format];
42
-
41
+
43
42
// AudioEngine allows us to attach and connect nodes at runtime but with few limitations
44
43
// in this case if it is the first player and recorder started the engine we need to restart.
45
44
// It can be optimized by tracking if we haven't break rules of at runtime modifications from docs
46
45
// https://developer.apple.com/documentation/avfaudio/avaudioengine?language=objc
47
46
//
48
47
// Currently we are restarting because we do not see any significant performance issue and case when
49
48
// 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 ];
51
52
}
52
53
53
54
- (void )stop
@@ -57,7 +58,6 @@ - (void)stop
57
58
AudioEngine *audioEngine = [AudioEngine sharedInstance ];
58
59
assert (audioEngine != nil );
59
60
[audioEngine detachSourceNodeWithId: self .sourceNodeId];
60
- [audioEngine restartAudioEngine ];
61
61
[audioEngine stopIfNecessary ];
62
62
self.sourceNodeId = nil ;
63
63
}
@@ -67,9 +67,8 @@ - (bool)resume
67
67
NSLog (@" [AudioPlayer] resume" );
68
68
AudioEngine *audioEngine = [AudioEngine sharedInstance ];
69
69
assert (audioEngine != nil );
70
- [audioEngine startEngine ];
71
70
72
- return [audioEngine startEngine ];
71
+ return [audioEngine startIfNecessary ];
73
72
}
74
73
75
74
- (void )suspend
0 commit comments