[绍棠] GPUImageMovieWriter 无法2次录像 报错:[AVAssetWriter startWriting] Cannot call method when status is 3
来源:互联网 发布:linux telnet测试端口 编辑:程序博客网 时间:2024/05/23 22:17
GPUImage官方的示例工程里有个SimpleVideoFilter,实时摄像头内容加上滤镜。然而这个工程写得非常简单,录像是自动启动的,录制10秒停止。
研究一下代码,发现录制代码在-(void)viewDidLoad最后部分,2个dispatch_after调用。第一个dispatch_after开始录制,然后里面一层的dispatch_after停止录制并保存到手机相册。明确了功能就可以把这2部分代码拿出来,放在一个button事件中。
然而,问题随之出现,按下start录制,stop停止。按钮又变成start,此时再按下去,会报错,status is 3(也有可能是2,数字是几不重要)。
查阅官方文档,了解一下status含义。
enum {
AVAssetWriterStatusUnknown = 0,
AVAssetWriterStatusWriting,
AVAssetWriterStatusCompleted,
AVAssetWriterStatusFailed,
AVAssetWriterStatusCancelled
};
3是AVAssetWriterStatusFailed。
报错是在[movieWriter startRecording];这行出现,追溯源码,可以看到GPUImageMovieWriter其实是对AssetWriter进行了一次封装,其核心的写文件的动作还是由AssetWriter完成。这个错误信息就是从AssetWriter发出的。
查看源码可以发现[movieWriter finishRecording];以后并没有新建一个AssetWriter实例。那么我们尽量不改动GPUImage源码,在使用上对自己的app逻辑进行改进。
示例代码使用ALAssetsLibrary,调用[library writeVideoAtPathToSavedPhotosAlbum:completionBlock:]来写入相册,在completionBlock中我们可以在最后加入几行代码
[_videoCamera removeTarget:_movieWriter];//1
[[NSFileManager defaultManager] removeItemAtURL:_movieURL error:nil];//2
[self initMovieWriter];//3
[_videoCamera addTarget:_movieWriter];//4
- (void)initMovieWriter {
_movieWriter = [[GPUImageMovieWriter alloc] initWithMovieURL:_movieURL size:CGSizeMake(720.0, 1280.0)];
_movieWriter.encodingLiveVideo = YES;
}
- (void)initMovieWriter是我自己提取出来的。封装一下初始化方法。
1.摄像头实例取消对GPUImageMovieWriter的绑定,因为重新实例化新的GPUImageMovieWriter以后原来的实例就没用了。
2.删除原来已经写好的影片文件,如果新的实例直接写入已存在的文件会报错AVAssetWriterStatusFailed。
3.重新实例化一个GPUImageMovieWriter。
4.把新的GPUImageMovieWriter绑定到摄像头实例。
这样我们就可以不停的摄像,保存,再摄像,再保存,再也不报错了。
- [绍棠] GPUImageMovieWriter 无法2次录像 报错:[AVAssetWriter startWriting] Cannot call method when status is 3
- 关于AVAssetWriter,AVAssetWriterInput报错Cannot call method when status is 2的问题记录
- 报错:Cannot call sendRedirect() after the response has been committed/HTTP Status 405
- jquery file upload报错:Uncaught TypeError: Cannot call method 'unshift' of undefined
- jquery file upload报错:Cannot call method 'parseMetaData' of undefined
- Chrome中canvas报Cannot call method 'getContext' of null
- android webView 图片加载不出来 后台报错Uncaught TypeError: Cannot call method 'getElementsByTagName' of null
- 逻辑没错但是报:Cannot call sendRedirect() after the response has been committed/HTTP Status 405
- Cannot call method in class, which is explicitly implemented from interface, through new object
- RecyclerView , IllegalStateException: Cannot call this method while RecyclerView is computing a layo
- Cannot call this method while RecyclerView is computing a layout or scrolling
- 使用RecyclerView中引发 java.lang.IllegalStateException: Cannot call this method while RecyclerView is com
- 关于cannot call this method while recyclerview is computing a layout or scrollin
- Call requires API level 3 (current min is 2) android导入程序报错
- butterknife报错 cannot find method "value" cannot resolve symbol
- 访问arcgisserver报:HTTP Status 405 - HTTP method GET is not supported by this URL
- FLAG_NEEDS_MENU_KEY cannot be resolved or is not a field 报错,程序无法运行
- Cannot call method 'indexOf' of undefined
- java设计模式-建造者模式
- 2017年8月18日15:20:10
- 菜鸟都能理解的KMP算法
- ROS中remap一个topic
- js学习笔记
- [绍棠] GPUImageMovieWriter 无法2次录像 报错:[AVAssetWriter startWriting] Cannot call method when status is 3
- 搭建日志分析系统ELK
- 带环链表找入口
- html+css面试题
- 关于小程序声明周期
- Linux设备树语法详解
- matplotlib的plot函数说明
- 记一道CTF题babyphp之学习代码注入
- PHP学习笔记3-php提高