How to record landscape video in a portrait application? (Swift 2, iPhone)

邮差的信 提交于 2019-12-19 04:44:14

问题


I need to record a landscape video inside my portrait iPhone application.

Is it possible or is it blocked by any hardware limitation?

Using AVCaptureVideoPreviewLayer I was able to change the preview orientation, but the video is still recorded in Portrait.

Any idea is welcomed!


回答1:


You should be able to set the orientation to landscape for the video recording. And set the preview layer to portrait to achieve a landscape video that is previewed in portrait. If I understand your question correctly. Can you show any code you tried already?

        if captureSession.canAddOutput(videoOutput) {
            captureSession.addOutput(videoOutput)
            let connection = videoOutput.connectionWithMediaType(AVMediaTypeVideo)
            if connection.supportsVideoOrientation {
                connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight
            }
            print("adding output")

        } else {
            print("Could not add front video output")
            return
        }

    ...

    func setupPreviewLayer() {

        ...etc...

        previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.Portrait

        ...etc...

    }



回答2:


I had a problem playing landscape video in portrait application. Solved it by writing the following code. Add the following code on configuring session:

captureSession.addOutput(videoFileOutput)
let videoDataOuputConnection = videoFileOutput.connection(with: .video)
videoFileOutput.setRecordsVideoOrientationAndMirroringChangesAsMetadataTrack(true, for: videoDataOuputConnection!)

In start recording video function, Add the following code before starting to record:

let videoDataOuputConnection = videoFileOutput.connection(with: .video)
let orientation = UIDevice.current.orientation
videoDataOuputConnection!.videoOrientation = AVCaptureVideoOrientation(rawValue: orientation.rawValue)!

Note: videoFileOutput is of type AVCaptureMovieFileOutput, captureSession is of type AVCaptureSession



来源:https://stackoverflow.com/questions/38072441/how-to-record-landscape-video-in-a-portrait-application-swift-2-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!