简体   繁体   English

AVAssetWriter文件备份和恢复

[英]AVAssetWriter file backup and recovery

I am using AVAssetWriter to write audio (and/or video) to a quicktime movie format. 我正在使用AVAssetWriter将音频(和/或视频)写入快速时间电影格式。 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). 似乎没有一种方法可以调用finishWritingWithCompletionHandler,然后在同一文件上附加一个新会话(即,没有可以找到的暂停,保存,恢复机制)。 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: 在Swift中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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