在Swiftui / avfoundation / avcapturedeviceinput中遇到麻烦
我正在使用Avfoundation对Swiftui进行编码,并能够根据预期工作。 我遇到了一个错误,在翻转后,它将变成黑屏
但是,由于我正在实施翻转摄像机功能,因此 我的代码
class CameraViewModel: NSObject,ObservableObject,AVCaptureFileOutputRecordingDelegate, AVCapturePhotoCaptureDelegate{
...
@Published var session = AVCaptureSession()
@objc dynamic var videoDeviceInput: AVCaptureDeviceInput!
private let sessionQueue = DispatchQueue(label: "session queue")
func setUp(){
do{
self.session.beginConfiguration()
let cameraDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front)
let videoInput = try AVCaptureDeviceInput(device: cameraDevice!)
let audioDevice = AVCaptureDevice.default(for: .audio)
let audioInput = try AVCaptureDeviceInput(device: audioDevice!)
// MARK: Audio Input
if self.session.canAddInput(videoInput) && self.session.canAddInput(audioInput){
self.session.addInput(videoInput)
self.session.addInput(audioInput)
self.videoDeviceInput = videoInput
}
if self.session.canAddOutput(self.output){
self.session.addOutput(self.output)
}
if self.session.canAddOutput(self.photoOutput){
self.session.addOutput(self.photoOutput)
}
self.session.commitConfiguration()
}
catch{
print(error.localizedDescription)
}
}
func changeCamera() {
sessionQueue.async {
if self.videoDeviceInput != nil {
let currentVideoDevice = self.videoDeviceInput.device
let currentPosition = currentVideoDevice.position
let preferredPosition: AVCaptureDevice.Position
switch currentPosition {
case .unspecified, .front:
preferredPosition = .back
case .back:
preferredPosition = .front
@unknown default:
print("Unknown capture position. Defaulting to back, dual-camera.")
preferredPosition = .back
}
print("current pos is \(currentPosition.rawValue) and preferred position is \(preferredPosition.rawValue)")
do{
self.session.beginConfiguration()
//remove device as needed
self.session.removeInput(self.videoDeviceInput)
let newCameraDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: preferredPosition)
let newVideoInput = try AVCaptureDeviceInput(device: newCameraDevice!)
let newAudioDevice = AVCaptureDevice.default(for: .audio)
let newAudioInput = try AVCaptureDeviceInput(device: newAudioDevice!)
// MARK: Audio Input
if self.session.canAddInput(newVideoInput) && self.session.canAddInput(newAudioInput){
self.session.addInput(newVideoInput)
self.session.addInput(newAudioInput)
self.videoDeviceInput = newVideoInput
}
self.session.commitConfiguration()
}catch{
print(error.localizedDescription)
}
}
}
}
}
我不确定我在做什么错,因为我已经查找了以前的堆栈溢出线程和在线资源,他们所说的就是获取设备输入位置并更改该位置,然后在进行配置之前将其删除。任何帮助将不胜感激!
编辑:我发现解决方案是摆脱音频输入代码,因为没有它的音频仍在捕获
I am coding a camera with swiftui using avfoundation and was able to get the setup to work as intended. However, as I'm implementing a flip camera functionality I'm running into an error where after flipping it just goes to a black screen as I'm assuming the input gets removed but the correct flipped input doesn't get shown:
Here is my code
class CameraViewModel: NSObject,ObservableObject,AVCaptureFileOutputRecordingDelegate, AVCapturePhotoCaptureDelegate{
...
@Published var session = AVCaptureSession()
@objc dynamic var videoDeviceInput: AVCaptureDeviceInput!
private let sessionQueue = DispatchQueue(label: "session queue")
func setUp(){
do{
self.session.beginConfiguration()
let cameraDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front)
let videoInput = try AVCaptureDeviceInput(device: cameraDevice!)
let audioDevice = AVCaptureDevice.default(for: .audio)
let audioInput = try AVCaptureDeviceInput(device: audioDevice!)
// MARK: Audio Input
if self.session.canAddInput(videoInput) && self.session.canAddInput(audioInput){
self.session.addInput(videoInput)
self.session.addInput(audioInput)
self.videoDeviceInput = videoInput
}
if self.session.canAddOutput(self.output){
self.session.addOutput(self.output)
}
if self.session.canAddOutput(self.photoOutput){
self.session.addOutput(self.photoOutput)
}
self.session.commitConfiguration()
}
catch{
print(error.localizedDescription)
}
}
func changeCamera() {
sessionQueue.async {
if self.videoDeviceInput != nil {
let currentVideoDevice = self.videoDeviceInput.device
let currentPosition = currentVideoDevice.position
let preferredPosition: AVCaptureDevice.Position
switch currentPosition {
case .unspecified, .front:
preferredPosition = .back
case .back:
preferredPosition = .front
@unknown default:
print("Unknown capture position. Defaulting to back, dual-camera.")
preferredPosition = .back
}
print("current pos is \(currentPosition.rawValue) and preferred position is \(preferredPosition.rawValue)")
do{
self.session.beginConfiguration()
//remove device as needed
self.session.removeInput(self.videoDeviceInput)
let newCameraDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: preferredPosition)
let newVideoInput = try AVCaptureDeviceInput(device: newCameraDevice!)
let newAudioDevice = AVCaptureDevice.default(for: .audio)
let newAudioInput = try AVCaptureDeviceInput(device: newAudioDevice!)
// MARK: Audio Input
if self.session.canAddInput(newVideoInput) && self.session.canAddInput(newAudioInput){
self.session.addInput(newVideoInput)
self.session.addInput(newAudioInput)
self.videoDeviceInput = newVideoInput
}
self.session.commitConfiguration()
}catch{
print(error.localizedDescription)
}
}
}
}
}
I'm not sure what I'm doing wrong as I've looked up previous stack overflow threads and online resources and all they say is to get the device input position and change that, remove it before committing configuration. Any help will be greatly appreciated!
Edit: I found the solution was to get rid of the audio input code as the audio is still being captured without it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于您未显示的代码中。
当您在SwiftUI视图中显示图像时,方向取决于二手相机。
对于前摄像头,它是
.upmrored
。如果切换到使用后相机,则需要使用
.up
。The problem is in the code you are not showing.
When you are displaying the Image in your SwiftUI view, the orientation depends on the used camera.
For the front camera, it is
.upMirrored
.If you switch to use the back camera, you need to use
.up
.