2016-04-21 17 views
0

Ich entwickle gerade eine App, die ein Video aufzeichnet und Ihnen dann das Video zeigt, um zu überprüfen, ob das Video gut genug ist. Wenn das aufgezeichnete Video jedoch angezeigt wird, wird es in der falschen Ausrichtung angezeigt.AVPlayerLayer wird nicht korrekt angezeigt

Also im Grunde nehme ich es im LandscapeRight-Modus auf. Aber wenn es angezeigt wird, zeigt es es im Hochformat an und nimmt es anscheinend auch im Anzeigemodus auf. Auch wenn ich AVCaptureVideoOrientation.LandscapeRight einstelle.

Hier ist der Code, ich bin mit der Aufnahme zu Setup:

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() 

    } 

Danach wird der nächste Delegierte aufgerufen wird, und ich zeige es in derselben Ansicht, aber für eine Wiedergabe:

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() 
    } 

Jetzt zeigt die Wiedergabe es in der falschen Ausrichtung an, weiß jemand, wie man das repariert? Ich habe auch die Ausrichtung des Viewcontrollers zu LandscapeRight.

Antwort

0

Lösung:

// 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)! 
       } 
      } 
     } 

Grundsätzlich müssen Sie die AVCaptureVideoOrientation der Verbindung mit der richtigen Ausrichtung setzen, bevor Sie die Aufnahme starten. Sonst könnte es in der falschen Ausrichtung aufzeichnen.