@@ -18,7 +18,8 @@ final public class ScreenRecorder {
1818 private var videoOutputURL : URL ?
1919 private var videoWriter : AVAssetWriter ?
2020 private var videoWriterInput : AVAssetWriterInput ?
21- private var audioWriterInput : AVAssetWriterInput ?
21+ private var micAudioWriterInput : AVAssetWriterInput ?
22+ private var appAudioWriterInput : AVAssetWriterInput ?
2223 private var saveToCameraRoll = false
2324 let recorder = RPScreenRecorder . shared ( )
2425
@@ -40,7 +41,8 @@ final public class ScreenRecorder {
4041 errorHandler: @escaping ( Error ) -> Void ) {
4142 createVideoWriter ( in: outputURL, error: errorHandler)
4243 addVideoWriterInput ( size: size)
43- addAudioWriterInput ( )
44+ self . micAudioWriterInput = createAndAddAudioInput ( )
45+ self . appAudioWriterInput = createAndAddAudioInput ( )
4446 startCapture ( error: errorHandler)
4547 }
4648
@@ -89,8 +91,8 @@ final public class ScreenRecorder {
8991 newVideoWriterInput. expectsMediaDataInRealTime = true
9092 videoWriter? . add ( newVideoWriterInput)
9193 }
92-
93- private func addAudioWriterInput ( ) {
94+
95+ private func createAndAddAudioInput ( ) -> AVAssetWriterInput {
9496 let settings = [
9597 AVFormatIDKey: Int ( kAudioFormatMPEG4AAC) ,
9698 AVSampleRateKey: 12000 ,
@@ -99,10 +101,11 @@ final public class ScreenRecorder {
99101 ]
100102
101103 let audioInput = AVAssetWriterInput ( mediaType: . audio, outputSettings: settings)
102- self . audioWriterInput = audioInput
103104
104105 audioInput. expectsMediaDataInRealTime = true
105106 videoWriter? . add ( audioInput)
107+
108+ return audioInput
106109 }
107110
108111 private func startCapture( error: @escaping ( Error ) -> Void ) {
@@ -115,10 +118,10 @@ final public class ScreenRecorder {
115118 switch sampleType {
116119 case . video:
117120 self . handleSampleBuffer ( sampleBuffer: sampleBuffer)
118- case . audioApp, . audioMic :
119- if self . audioWriterInput ? . isReadyForMoreMediaData ?? false {
120- self . audioWriterInput ? . append ( sampleBuffer )
121- }
121+ case . audioApp:
122+ self . add ( sample : sampleBuffer , to : self . appAudioWriterInput )
123+ case . audioMic :
124+ self . add ( sample : sampleBuffer , to : self . micAudioWriterInput )
122125 default :
123126 break
124127 }
@@ -134,6 +137,12 @@ final public class ScreenRecorder {
134137 self . videoWriterInput? . append ( sampleBuffer)
135138 }
136139 }
140+
141+ private func add( sample: CMSampleBuffer , to writerInput: AVAssetWriterInput ? ) {
142+ if writerInput? . isReadyForMoreMediaData ?? false {
143+ writerInput? . append ( sample)
144+ }
145+ }
137146
138147 /**
139148 Stops recording the content of the application screen, after calling startRecording
@@ -148,7 +157,8 @@ final public class ScreenRecorder {
148157 } )
149158
150159 self . videoWriterInput? . markAsFinished ( )
151- self . audioWriterInput? . markAsFinished ( )
160+ self . micAudioWriterInput? . markAsFinished ( )
161+ self . appAudioWriterInput? . markAsFinished ( )
152162 self . videoWriter? . finishWriting {
153163 self . saveVideoToCameraRollAfterAuthorized ( errorHandler: errorHandler)
154164 }
0 commit comments