@@ -87,7 +87,7 @@ - (instancetype)initWithCameraName:(NSString *)cameraName
87
87
- (void )start ;
88
88
- (void )stop ;
89
89
- (void )startRecordingVideoAtPath : (NSString *)path result : (FlutterResult)result ;
90
- - (void )stopRecordingVideo ;
90
+ - (void )stopRecordingVideoWithResult : (FlutterResult) result ;
91
91
- (void )captureToFile : (NSString *)filename result : (FlutterResult)result ;
92
92
@end
93
93
@@ -139,7 +139,6 @@ - (instancetype)initWithCameraName:(NSString *)cameraName
139
139
[_captureSession addConnection: connection];
140
140
_capturePhotoOutput = [AVCapturePhotoOutput new ];
141
141
[_captureSession addOutput: _capturePhotoOutput];
142
-
143
142
return self;
144
143
}
145
144
@@ -176,7 +175,7 @@ - (void)captureOutput:(AVCaptureOutput *)output
176
175
}
177
176
}
178
177
if (!CMSampleBufferDataIsReady (sampleBuffer)) {
179
- NSLog ( @" sample buffer is not ready. Skipping sample" );
178
+ _eventSink (@{ @" event " : @" error " , @" errorDescription " : @" sample buffer is not ready. Skipping sample" } );
180
179
return ;
181
180
}
182
181
if (_isRecording == YES ) {
@@ -194,30 +193,25 @@ - (void)captureOutput:(AVCaptureOutput *)output
194
193
}
195
194
196
195
- (void )newVideoSample : (CMSampleBufferRef)sampleBuffer {
197
- if (_isRecording) {
198
196
if (_videoWriter.status > AVAssetWriterStatusWriting) {
199
- NSLog ( @" Warning: writer status is %ld " , ( long ) _videoWriter.status );
200
- if (_videoWriter. status == AVAssetWriterStatusFailed) NSLog ( @" Error: %@ " , _videoWriter.error );
197
+ if ( _videoWriter.status == AVAssetWriterStatusFailed)
198
+ _eventSink (@{ @" event " : @" error " , @" errorDescription " : [ NSString stringWithFormat: @" %@ " , _videoWriter.error]} );
201
199
return ;
202
200
}
203
201
if (![_videoWriterInput appendSampleBuffer: sampleBuffer]) {
204
- NSLog ( @" Unable to write to video input" );
202
+ _eventSink (@{ @" event " : @" error " , @" errorDescription " : [ NSString stringWithFormat: @" %@ " , @" Unable to write to video input" ]} );
205
203
}
206
- }
207
204
}
208
205
209
206
- (void )newAudioSample : (CMSampleBufferRef)sampleBuffer {
210
- if (_isRecording) {
211
207
if (_videoWriter.status > AVAssetWriterStatusWriting) {
212
- NSLog ( @" Warning: writer status is %ld " , ( long ) _videoWriter.status );
213
- if (_videoWriter. status == AVAssetWriterStatusFailed) NSLog ( @" Error: %@ " , _videoWriter.error );
208
+ if ( _videoWriter.status == AVAssetWriterStatusFailed)
209
+ _eventSink (@{ @" event " : @" error " , @" errorDescription " : [ NSString stringWithFormat: @" %@ " , _videoWriter.error]} );
214
210
return ;
215
211
}
216
-
217
212
if (![_audioWriterInput appendSampleBuffer: sampleBuffer]) {
218
- NSLog ( @" Unable to write to audio input" );
213
+ _eventSink (@{ @" event " : @" error " , @" errorDescription " : [ NSString stringWithFormat: @" %@ " , @" Unable to write to audio input" ]} );
219
214
}
220
- }
221
215
}
222
216
223
217
- (void )close {
@@ -255,50 +249,47 @@ - (FlutterError *_Nullable)onListenWithArguments:(id _Nullable)arguments
255
249
return nil ;
256
250
}
257
251
- (void )startRecordingVideoAtPath : (NSString *)path result : (FlutterResult)result {
258
- if (!_isAudioSetup) {
259
- [self setUpCaptureSessionForAudio ];
260
- }
261
252
262
253
if (!_isRecording) {
263
- NSLog (@" start video recording..." );
264
254
if (![self setupWriterForPath: path]) {
265
- NSLog ( @" Setup Writer Failed" );
255
+ _eventSink (@{ @" event " : @" error " , @" errorDescription " : @" Setup Writer Failed" } );
266
256
return ;
267
257
}
268
- [_captureSession stopRunning ];
258
+ [_captureSession stopRunning ];
269
259
_isRecording = YES ;
270
- [_captureSession startRunning ];
260
+ [_captureSession startRunning ];
271
261
}
272
- // NSLog(@"Saved: %@ ", path );
273
262
}
274
- - (void )stopRecordingVideo {
275
- if (_isRecording)
276
263
264
+ - (void )stopRecordingVideoWithResult : (FlutterResult)result {
265
+ if (_isRecording)
277
266
{
278
267
_isRecording = NO ;
279
- [_captureSession stopRunning ] ;
268
+ __block NSString *path = _videoWriter. outputURL . absoluteString ;
280
269
if (_videoWriter.status != 0 ) {
281
270
[_videoWriter finishWritingWithCompletionHandler: ^{
282
- [_captureSession startRunning ] ;
271
+ result (@{ @" outputURL " :path}) ;
283
272
}];
284
273
}
285
- NSLog (@" video recording stopped" );
286
274
}
287
275
}
276
+
288
277
- (BOOL )setupWriterForPath : (NSString *)path {
289
278
NSError *error = nil ;
290
- NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
291
- NSString *documentsDirectoryPath = [paths objectAtIndex: 0 ];
292
- time_t unixTime = (time_t )[[NSDate date ] timeIntervalSince1970 ];
293
- NSString *timestamp = [NSString stringWithFormat: @" %ld " , unixTime];
294
- NSString *filename = [NSString stringWithFormat: @" iPhoneVideo_%@ .mp4" , timestamp];
295
- NSURL *outputURL =
296
- [NSURL fileURLWithPath: [documentsDirectoryPath stringByAppendingPathComponent: filename]];
297
-
279
+ NSURL *outputURL;
298
280
if (path != nil ) {
299
281
outputURL = [NSURL fileURLWithPath: path];
300
282
}
301
-
283
+ else
284
+ {
285
+ NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
286
+ NSString *documentsDirectoryPath = [paths objectAtIndex: 0 ];
287
+ time_t unixTime = (time_t )[[NSDate date ] timeIntervalSince1970 ];
288
+ NSString *timestamp = [NSString stringWithFormat: @" %ld " , unixTime];
289
+ NSString *filename = [NSString stringWithFormat: @" iPhoneVideo_%@ .mp4" , timestamp];
290
+ outputURL =
291
+ [NSURL fileURLWithPath: [documentsDirectoryPath stringByAppendingPathComponent: filename]];
292
+ }
302
293
_videoWriter =
303
294
[[AVAssetWriter alloc ] initWithURL: outputURL fileType: AVFileTypeQuickTimeMovie error: &error];
304
295
NSParameterAssert (_videoWriter);
@@ -308,18 +299,15 @@ - (BOOL)setupWriterForPath:(NSString *)path {
308
299
[NSNumber numberWithInt: _previewSize.height], AVVideoWidthKey,
309
300
[NSNumber numberWithInt: _previewSize.width], AVVideoHeightKey,
310
301
nil ];
311
-
312
302
_videoWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType: AVMediaTypeVideo
313
303
outputSettings: videoSettings];
314
-
315
304
NSParameterAssert (_videoWriterInput);
316
305
_videoWriterInput.expectsMediaDataInRealTime = YES ;
317
306
318
307
// Add the audio input
319
308
AudioChannelLayout acl;
320
309
bzero (&acl, sizeof (acl));
321
310
acl.mChannelLayoutTag = kAudioChannelLayoutTag_Mono ;
322
-
323
311
NSDictionary *audioOutputSettings = nil ;
324
312
// Both type of audio inputs causes output video file to be corrupted.
325
313
audioOutputSettings = [NSDictionary
@@ -328,14 +316,11 @@ - (BOOL)setupWriterForPath:(NSString *)path {
328
316
[NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey,
329
317
[NSData dataWithBytes: &acl length: sizeof (acl)],
330
318
AVChannelLayoutKey, nil ];
331
-
332
319
_audioWriterInput = [AVAssetWriterInput assetWriterInputWithMediaType: AVMediaTypeAudio
333
320
outputSettings: audioOutputSettings];
334
321
_audioWriterInput.expectsMediaDataInRealTime = YES ;
335
-
336
322
[_videoWriter addInput: _videoWriterInput];
337
- [_videoWriter addInput: _audioWriterInput];
338
-
323
+ [_videoWriter addInput: _audioWriterInput];
339
324
dispatch_queue_t queue = dispatch_queue_create (" MyQueue" , NULL );
340
325
[_captureVideoOutput setSampleBufferDelegate: self queue: queue];
341
326
[_audioOutput setSampleBufferDelegate: self queue: queue];
@@ -359,7 +344,7 @@ - (void)setUpCaptureSessionForAudio {
359
344
[_captureSession addOutput: _audioOutput];
360
345
_isAudioSetup = YES ;
361
346
} else {
362
- NSLog ( @" Error: Unable to add Audio input/output to session capture" );
347
+ _eventSink (@{ @" event " : @" error " , @" errorDescription " : @" Unable to add Audio input/output to session capture" } );
363
348
_isAudioSetup = NO ;
364
349
}
365
350
}
@@ -457,8 +442,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
457
442
@" captureHeight" : @(cam.captureSize .height ),
458
443
});
459
444
// starting the choosen cam
460
- FLTCam *cam2 = _cams[@(textureId)];
461
- [cam2 start ];
445
+ [cam start ];
462
446
}
463
447
} else {
464
448
NSDictionary *argsMap = call.arguments ;
@@ -476,7 +460,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result
476
460
[cam startRecordingVideoAtPath: call.arguments[@" filePath" ] result: result];
477
461
478
462
} else if ([@" stopVideoRecording" isEqualToString: call.method]) {
479
- [cam stopRecordingVideo ];
463
+ [cam stopRecordingVideoWithResult: result ];
480
464
result (nil );
481
465
} else {
482
466
result (FlutterMethodNotImplemented);
0 commit comments