简体   繁体   中英

AVAssetWriter file backup and recovery

I am using AVAssetWriter to write audio (and/or video) to a quicktime movie format. How do I periodically (every couple minutes) save (or backup) this file without stopping the session (ie, keep on recording)?

Here is the code I am using to copy the file during recording:

NSString* filename = [NSString stringWithFormat:@"mediaFile.mp4"];
NSString* path = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
NSURL* existingURL = [NSURL fileURLWithPath:path];

path = [myAppDocumentsDirectory stringByAppendingPathComponent:filename];
NSURL* copyURL = [NSURL fileURLWithPath:path];

[[NSFileManager defaultManager] copyItemAtURL:existingURL toURL:copyURL error:nil];

The file is created but appears to be unreadable unless I invoke something like:

[assetWriter finishWritingWithCompletionHandler: handler];

So what is the mechanism for backing up a file while still recording? There does not appear to be a way to call finishWritingWithCompletionHandler and then append a new session on the same file (ie, there is no pause, save, resume mechanism that I can find). Perhaps there is a way to properly close or 'finish' the file after the copy?

If a use case is needed, one might be where the app is shut down by the system due to a low battery or system crash. Restarting the app should then allow the user to recover this file and play it back.

I found the solution. You have to write this before to start to record. This will save the file each interval of time. Then if your app crash, the video will be playable.

[_assetWriter setMovieFragmentInterval:CMTimeMake(1, 600)];

in Swift:

_assetWriter?.movieFragmentInterval = CMTime(value: 1, timescale: 600)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM