- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个自定义摄像头,我正在尝试添加一项功能,让用户可以在录制视频时在前后摄像头之间切换。我现在的方法是在他们切换摄像头时停止并开始播放新视频,但它会切断视频的一小部分,我不确定为什么。我怎样才能让它像 snapchat 一样,以便它获得完整的视频,并且在他们切换相机时不会切断任何内容。到目前为止,这是我的代码
@objc func switchCameraInput() {
self.captureSession.beginConfiguration()
var existingConnection:AVCaptureDeviceInput!
for connection in self.captureSession.inputs {
let input = connection as! AVCaptureDeviceInput
if input.device.hasMediaType(AVMediaType.video) {
existingConnection = input
}
}
self.captureSession.removeInput(existingConnection)
turnFlashOff()
var newCamera:AVCaptureDevice!
if let oldCamera = existingConnection {
if oldCamera.device.position == .back {
newCamera = self.cameraWithPosition(position: .front)
} else {
newCamera = self.cameraWithPosition(position: .back)
}
}
var newInput:AVCaptureDeviceInput!
do {
newInput = try AVCaptureDeviceInput(device: newCamera)
self.captureSession.addInput(newInput)
} catch {
ProgressHUD.showError(error.localizedDescription)
}
self.captureSession.commitConfiguration()
// This is where i handle switching while recording
if self.movieFileOutput.isRecording {
hasSwappedCamera = true
turnFlashOff()
//self.movieFileOutput.stopRecording()
self.movieFileOutput.connection(with: AVMediaType.video)?.videoOrientation = self.videoOrientation()
self.movieFileOutput.maxRecordedDuration = self.maxRecordedDuration()
self.movieFileOutput.startRecording(to: URL(fileURLWithPath:self.videoFileLocation()), recordingDelegate: self)
turnOnFlash()
}
}
最佳答案
自 the question我认为这将有助于回答您在 Objective-C 中的问题,而您更喜欢 Swift,我已经“翻译”了下面的所有代码。
请注意,我没有编译这个,并且知道有几件事不会编译启动。像 AVMediaTypeVideo
这样的枚举值在 Swift 中通常只是 .video
。另外,我很确定答案有一些不正确的代码,主要是将 isFrontRecording
和 isBackRecording
bool 值设置回 false
。我认为这些应该发生在 completionHandler
中,但如前所述,我没有编译它,所以对此持怀疑态度。我包括了那个问题(Objective-C)的所有代码以及我对 Swift 的快速和粗略的翻译。
不过,我希望这会有所帮助:)
objective-C :
/* Front camera settings */
@property bool isFrontRecording;
@property (strong, nonatomic) AVCaptureDeviceInput *videoInputBack;
@property (strong, nonatomic) AVCaptureStillImageOutput *imageOutputBack;
@property (strong, nonatomic) AVCaptureSession *sessionBack;
/* Back camera settings */
@property bool isBackRecording;
@property (strong, nonatomic) AVCaptureDeviceInput *videoInputFront;
@property (strong, nonatomic) AVCaptureStillImageOutput *imageOutputFront;
@property (strong, nonatomic) AVCaptureSession *sessionFront;
swift :
var isFrontRecording: Bool
var videoInputBack: AVCaptureDeviceInput
var imageOutputBack: AVCaptureStillImageOutput
var sessionBack: AVCaptureSession
var isBackRecording: Bool
var videoInputFront: AVCaptureDeviceInput
var imageOutputFront: AVCaptureStillImageOutput
var sessionFront: AVCaptureSession
objective-C
- (void)viewDidLoad {
[super viewDidLoad];
[self setupBackAVCapture];
self.isFrontRecording = NO;
self.isBackRecording = NO;
}
- (void)setupBackAVCapture
{
NSError *error = nil;
self.sessionBack = [[AVCaptureSession alloc] init];
self.sessionBack.sessionPreset = AVCaptureSessionPresetPhoto;
AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
self.videoInputBack = [[AVCaptureDeviceInput alloc] initWithDevice:camera error:&error];
[self.sessionBack addInput:self.videoInputBack];
self.imageOutputBack = [[AVCaptureStillImageOutput alloc] init];
[self.sessionBack addOutput:self.imageOutputBack];
}
swift :
override func viewDidLoad() {
super.viewDidLoad()
setupBackAVCapture()
isFrontRecording = false
isBackRecording = false
}
func setupBackAVCapture() {
var error: NSError = nil
sessionBack = AVCaptureSession()
sessionBack.sessionPreset = AVCaptureSessionPresetPhoto
let camera: AVCaptureDevice = AVCaptureDevice(defaultDeviceWithMediaType: AVMediaTypeVideo)
videoInputBack = AVCaptureDeviceInput(withDevice: camera, error: error)
sessionBack.addInput(videoInputBack)
imageOutputBack = AVCaptureStillImageOutput()
sessionBack.addOutput(imageOutputBack)
}
objective-C :
- (IBAction)buttonCapture:(id)sender {
[self takeBackPhoto];
}
- (void)takeBackPhoto
{
[self.sessionBack startRunning];
if (!self.isFrontRecording) {
self.isFrontRecording = YES;
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
AVCaptureConnection *videoConnection = [self.imageOutputBack connectionWithMediaType:AVMediaTypeVideo];
if (videoConnection == nil) {
return;
}
[self.imageOutputBack
captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer == NULL) {
return;
}
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
[self.imageView setImage:image];
[self.sessionBack stopRunning];
// Set up front camera setting and capture photo.
[self setupFrontAVCapture];
[self takeFrontPhoto];
}];
self.isFrontRecording = NO;
}
}
swift :
@IBOutlet func buttonCapture(sender: Any) {
takeBackPhoto()
}
func takeBackPhoto() {
sessionBack.startRunning()
if !isFrontRecording {
isFrontRecording = true
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
let videoConnection: AVCaptureConnection = imageOutputBack.connectionWithMediaType(AVMediaTypeVideo)
guard let videoConnection = videoConnection else {
return
}
imageOutputBack.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {
imageDataSampleBuffer: CMSSampleBufferRef, error: NSError in
guard let imageDataSampleBuffer = imageDataSampleBuffer else {
return
}
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
let image = UIImage(data: imageData)
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil)
self.imageView.setImage(image)
self.sessionback.stopRunning()
// Set up front camera setting and capture photo.
self.setupFronAVCapture()
self.takeFrontPhoto()
})
isFrontRecording = false
}
}
objective-C
- (void)setupFrontAVCapture
{
NSError *error = nil;
self.sessionFront = [[AVCaptureSession alloc] init];
self.sessionFront.sessionPreset = AVCaptureSessionPresetPhoto;
AVCaptureDevice *camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
camera = [self cameraWithPosition:AVCaptureDevicePositionFront];
self.videoInputFront = [[AVCaptureDeviceInput alloc] initWithDevice:camera error:&error];
[self.sessionFront addInput:self.videoInputFront];
self.imageOutputFront = [[AVCaptureStillImageOutput alloc] init];
[self.sessionFront addOutput:self.imageOutputFront];
}
- (void)takeFrontPhoto
{
[self.sessionFront startRunning];
if (!self.isBackRecording) {
self.isBackRecording = YES;
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
AVCaptureConnection *videoConnection = [self.imageOutputFront connectionWithMediaType:AVMediaTypeVideo];
if (videoConnection == nil) {
return;
}
[self.imageOutputFront
captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer == NULL) {
return;
}
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
[self.imageViewBack setImage:image];
[self.sessionFront stopRunning];
}];
self.isBackRecording = NO;
}
}
swift :
func setupFrontAVCapture() {
let error: NSError = nil
sessionFront = AVCaptureSession()
sessionFront.sessionPreset = AVCaptureSessionPresentPhoto
var camera: AVCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
camera = camera.cameraWithPosition(AVCaptureDevicePositionFront)
videoInputFront = AVCaptureDeviceInput(withDevice: camera, error: error)
sessionFront.addInput(videoInputFront)
imageOutputFront = AVCaptureStillImageOutput()
sessionFront.addOutput(imageOutputFront)
}
func takeFrontPhoto() {
sessionFront.startRunning()
if !isBackRecording {
isBackRecording = true
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
let videoConnection: AVCaptureConnection = imageOutputFront.connectionWithMediaType(AVMediaTypeVideo)
guard let videoConnection = videoConnection else {
return
}
imageOutputFront.captureStillImageAsynchronouslyFromConnection(videoConnection, completionHandler: {
imageDataSampleBuffer: CMSampleBufferRef, error: NSError in
guard let imageDataSampleBuffer = imageDataSampleBuffer else {
return
}
let imageData: NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
let image = UIImage(data: imageData)
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil)
self.imageViewBack.setImage(image)
self.sessionFront.stopRunning()
})
isBackRecording = false
}
}
祝你好运,让转换为你的项目工作!
关于ios - 录制视频时在前后摄像头之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47983572/
我编写了具有几个日志函数的日志帮助程序类。在 DEBUG 模式下一切正常。但是当我在 Release模式下运行我的代码时,它崩溃了。下面是代码片段: + (void)info:(NSString *)
在用 Python 编程时,如何使 VSCode 中的格式化程序使用制表符而不是空格进行缩进?我已经将 VSCode 设置为使用制表符,但是 Python 的格式化程序忽略了这一点并且只做它自己的事情
最初我的 mongod 进程运行良好。我发现我的虚拟机上有空间紧缩,所以删除了 2 个较旧的 oplog 文件以释放空间。然后接下来我启动 mongod 并开始出现错误。然后我尝试在本地数据库中修复我
CUDA 中的内核启动通常是异步的,这(据我了解)意味着一旦 CUDA 内核启动,控制权立即返回给 CPU。当 GPU 忙于数字运算时,CPU 继续做一些有用的工作除非使用 cudaThreadsyn
在 Angular View 之间链接时,是否有一种简单的方法可以保留查询参数? 当我使用 ngHref 时,查询参数丢失: Other page 我从这个网址导航 http://localhost/
nntp(新闻)打开错误:“>> 新闻/nntp 名称或服务未知)”。 我正在运行 ubuntu 最佳答案 Gnus 要求您配置要使用的新闻服务器,并且我认为它默认为“新闻”,它在您的本地搜索域中不存
有没有一种方法可以使 vim 突出显示具有给定颜色的制表符,但仅限于 expandtab选项设置了吗?我知道 'listchars' ,但我希望能够区分“有效标签”和“无效标签”。 最佳答案 您可以添
我对 Pandoc 比较陌生,我正在尝试用我的出版物生成一个 HTML 文件以放在我的网站上。我希望首先按年份对出版物列表进行编号和组织,最新的排在最前面,最早的排在最后。 我可以使用正确的 csl
根据 this question和我读过的文档,Spark Streaming 的 foreachRDD(someFunction) 将让 someFunction 本身仅在驱动程序进程中执行,但如果
我使用 stat_summary 从我的数据创建了多个条形图。但是,我想手动指定误差线的限制(而不是使用 mean_cl_boot)。对于使用 facet_grid 绘制的数据,如何做到这一点? 我用
我在 Ubuntu 下的 Bash 中运行 R。有没有办法在运行 R 时在 Bash 中使用颜色语法高亮显示?我将非常感谢有关该主题的任何信息,并希望这个问题不会令人讨厌。 最佳答案 您可以试试 co
在我的 Symfony2 应用程序中,我设置了防火墙,以便 /admin 下的所有内容路由需要通过 https 运行,但是在部署时我得到一个重定向循环。我已经阅读了 Symfony2 网站上关于防火墙
我在查询中给出了以下代码。 where to_date(cal_wid,'yyyymmdd') between add_months(to_date(sysdate, 'MM-YYY
我有一个动态 NSTableView,它可以根据提供的数据添加许多列。对于每一列,我将标题单元格设置为 NSPopUpButtonCell。 (旁注:我必须为 NSTableHeaderView 使用
我正在尝试创建一个嵌套列表。当点击时应将无序列表附加到 被点击了。 点击第一个后我的代码失败并且不会将列表附加到新插入的 。我想将无序列表添加到任何 就在列表中。 这是一个 fiddle :http
在 MyActivity 中,我正在尝试登录后端服务器。这是我尝试过的: myViewModel = new ViewModelProvider(this, factory).get(MyViewMo
有没有办法在打印 html 时在 javascript 中添加新行?这样打印出来的html是缩进的。 document.getElementById("id").innerHTML = "hello"
我正在使用 glDrawElements 绘制三角形网格,并且希望能够使用鼠标单击来拾取/选择三角形。三角形的网格可以很大。 在固定功能 OpenGL 中,可以使用 GL_SELECT: http:/
下面的可重现数据包含每个动物(猫和狗)在每个季节(夏季和冬季)的两个协变量(cov1 和 cov2)及其各自的误差估计值 (SE) 的 50 个观察值。 library(ggplot2); libra
我有多个按钮,每个按钮都有相关文本,当用户单击按钮时,文本应根据按钮更改,并且文本应显示在 DIV 中。 我使用 if elseif 在每个按钮的文本之间进行选择,现在我无法通过该函数将文本传递给 d
我是一名优秀的程序员,十分优秀!