rFlex / SCRecorder

iOS camera engine with Vine-like tap to record, animatable filters, slow motion, segments editing
Apache License 2.0
3.06k stars 583 forks source link

Cannot Save a Video the Third Time #292

Closed jakejin closed 8 years ago

jakejin commented 8 years ago

Saving a video first two time works fine. However, the third time it always gives an error:

Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not be completed" UserInfo=0x1db05250 {NSLocalizedDescription=The operation could not be completed, NSUnderlyingError=0x1db155d0 "The operation couldn’t be completed. (OSStatus error -12983.)", NSLocalizedFailureReason=An unknown error occurred (-12983)}

I looked at issue post #183 which had the same error message, but the solution they found did not work for me.

Does anyone know the issue that I am facing?

Here is my code:

SCFilter *currentFilter = [self.filterSwitcherView.selectedFilter copy];
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
NSString *fileName = [FileUtils getGlobalUniqueFileNameForType:FileTypeMP4];
NSURL *outputUrl = [[FileUtils videosDirectoryUrl] URLByAppendingPathComponent:fileName];
SCAssetExportSession *exportSession = [[SCAssetExportSession alloc] initWithAsset:self.asset];
exportSession.videoConfiguration.filter = currentFilter;
exportSession.videoConfiguration.preset = SCPresetHighestQuality;
exportSession.audioConfiguration.preset = SCPresetMediumQuality;
exportSession.videoConfiguration.maxFrameRate = 35;
exportSession.outputUrl = outputUrl; // Use temp url for application videos  self.recordSession.outputUrl;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.delegate = self;

[exportSession exportAsynchronouslyWithCompletionHandler:^{
    [[UIApplication sharedApplication] endIgnoringInteractionEvents];

    [self saveVideoWithURL:exportSession.outputUrl andFileName:fileName withError:exportSession.error];
}];
mitchellporter commented 8 years ago

This can be caused by various things and depends on your situation, but the root cause is a memory issue. I had the same thing happen last week. The view controller that owned my recorder was not being deallocated due to a custom transition error.

Check this out: http://stackoverflow.com/a/34649381/3344977

AV Foundation limits the number of instances you can have of certain classes. So if i had to guess I'd say that you've created too many SCAssetExportSessions.

jakejin commented 8 years ago

Hey thanks for your response, I agree that the root cause is a memory issue. However, I could not get down to the solution yet. The delegate method:

- (void)player:(SCPlayer *__nonnull)player itemReadyToPlay:(AVPlayerItem *__nonnull)item
{
    DDLogDebug(@"Player is ready to play: %@ -> %@", player, item);
}

gets called three times when I try to save the 2nd time and the 3rd time. Do you know why that might be happening?

Appreciate the help, thanks.

mitchellporter commented 8 years ago

Its probably getting called 3 times because there are 3 separate players in existence, and they are each calling their delegate method. You can use a tool like FLEX to easily see how many instances of a specific class are currently in memory. Try using it real quick and see if you have more than one player in existence.

jakejin commented 8 years ago

Thanks a lot, that library really helped. I was creating instances of the recorder every time that view controller popped up and it never deallocated.

Thanks!

mitchellporter commented 8 years ago

Glad I could help!