AVPlayerLayer无法以正确的方向显示

时间:2016-04-21 12:22:34

标签: ios avplayer avcapturesession avplayerlayer

我目前正在开发一款录制视频的应用,然后会向您显示视频以检查视频是否足够好。但是,在显示录制的视频时,它会以错误的方向显示..

所以基本上我用LandscapeRight模式记录它。但是在显示时它以纵向模式显示,并且显然也以显示模式记录它。即使我将它设置为AVCaptureVideoOrientation.LandscapeRight。

以下是我用来设置录音的代码:

func setupAVCapture(){
        session =  AVCaptureSession()
        session.sessionPreset = AVCaptureSessionPresetHigh

        let devices = AVCaptureDevice.devices()
        // Loop through all the capture devices on this phone
        for device in devices {
            // Make sure this particular device supports video
            if (device.hasMediaType(AVMediaTypeVideo)) {
                // Finally check the position and confirm we've got the front camera
                if(device.position == AVCaptureDevicePosition.Front) {
                    captureDevice = device as? AVCaptureDevice
                    if captureDevice != nil {
                        beginSession()
                        break
                    }
                }
            }
        }
    }

    func beginSession(){
        var err : NSError? = nil
        var deviceInput:AVCaptureDeviceInput?
        do {
            deviceInput = try AVCaptureDeviceInput(device: captureDevice)
        } catch let error as NSError {
            err = error
            deviceInput = nil
        };
        if err != nil {
            print("error: \(err?.localizedDescription)")
        }
        if self.session.canAddInput(deviceInput){
            self.session.addInput(deviceInput)
        }


        self.videoDataOutput = AVCaptureVideoDataOutput()
        self.videoDataOutput.alwaysDiscardsLateVideoFrames=true
        self.videoDataOutputQueue = dispatch_queue_create("VideoDataOutputQueue", DISPATCH_QUEUE_SERIAL)
        self.videoDataOutput.setSampleBufferDelegate(self, queue:self.videoDataOutputQueue)
        if session.canAddOutput(self.videoDataOutput){
            session.addOutput(self.videoDataOutput)
        }
        self.videoDataOutput.connectionWithMediaType(AVMediaTypeVideo).enabled = true

        self.previewLayer = AVCaptureVideoPreviewLayer(session: self.session)
        self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        self.previewLayer.frame = self.view.bounds
        self.previewLayer.masksToBounds = true
        self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientation.LandscapeRight


        let rootLayer :CALayer = CameraPreview.layer
        rootLayer.masksToBounds=true
        rootLayer.addSublayer(self.previewLayer)
        session.startRunning()

    }

在此之后,下一个委托被调用,我在同一个视图中显示它,但是为了播放:

func captureOutput(captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAtURL outputFileURL: NSURL!, fromConnections connections: [AnyObject]!, error: NSError!) {
        playbackAvailable = true
        SaveButton.hidden = false
        recording = false

        stopCamera()

//        let library = PHPhotoLibrary.sharedPhotoLibrary()
//        library.performChanges({ PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(outputFileURL)! }, completionHandler: {success, error in debugPrint("Finished saving asset. %@", (success ? "Success." : error!)) })

        // Play Video
        player = AVPlayer(URL: outputFileURL)
        playerLayer = AVPlayerLayer(player: player)
        playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
        playerLayer.masksToBounds = true
        playerLayer.frame = self.view.bounds
        CameraPreview.layer.addSublayer(playerLayer)
        player.play()
    }

现在播放以错误的方向显示,有人知道如何解决这个问题吗?我还将viewcontroller的方向定位到LandscapeRight。

1 个答案:

答案 0 :(得分:0)

解决方案:

// Neccesary to record in the correct orientation
        // ** MUST BE IMPLEMENTED AFTER SETING UP THE MOVIEFILE **
        var videoConnection: AVCaptureConnection? = nil
        if let connections = videoFileOutput.connections{
            for x in connections {
                if let connection = x as? AVCaptureConnection{
                    for port in connection.inputPorts{
                        if(port.mediaType == AVMediaTypeVideo){
                            videoConnection = connection
                        }
                    }
                }
            }
        }
        if(videoConnection != nil){
            if let vidConnect: AVCaptureConnection = videoConnection!{
                if(vidConnect.supportsVideoOrientation){
                    vidConnect.videoOrientation = AVCaptureVideoOrientation(rawValue: UIApplication.sharedApplication().statusBarOrientation.rawValue)!
                }
            }
        }

基本上,您需要在开始录制之前将连接的AVCaptureVideoOrientation设置为正确的方向。否则它可能会以错误的方向记录。