2017-07-18 10 views
0

これは再生しようとしているビデオファイルの例です: file:/// private/var/mobile/Containers/Data/Application/7725BCCA-B709-48FB-8FE3-DBC9F4B679C0/tmp/9AD6A48E -6A25-4114-88D3-474A0E1C762F.mp4録画した動画を再生するには?

録画していると思いますが、録画したビデオを再生しようとすると空白の画面になります。

func startRecording() { 

     print("start") 
     if movieOutput.isRecording == false { 

      let connection = movieOutput.connection(withMediaType: AVMediaTypeVideo) 

      if (connection?.isVideoOrientationSupported)! { 

       connection?.videoOrientation = currentVideoOrientation() 
      } 

      if (connection?.isVideoStabilizationSupported)! { 
       connection?.preferredVideoStabilizationMode = AVCaptureVideoStabilizationMode.auto 
      } 
      print(AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)) 


      let device = activeInput.device 
      if (device?.isSmoothAutoFocusSupported)! { 

       do { 
        try device?.lockForConfiguration() 
        device?.isSmoothAutoFocusEnabled = false 
        device?.unlockForConfiguration() 

       } catch { 
        print("Error setting configuration: \(error)") 
       } 

      } 

      outputURL = tempURL() 
      movieOutput.startRecording(toOutputFileURL: outputURL, recordingDelegate: self) 



     } else { 
      print("stop") 
      stopRecording() 
     } 

    } 


override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if segue.identifier == "showPreview" { 

      let previewVC = segue.destination as! ProfilePhotoPreviewViewController 
      previewVC.image = self.image 

     } else if segue.identifier == "showVideoPreview" { 
      let vc = segue.destination as! ProfilePhotoPreviewViewController 
      vc.videoURL = videoRecorded 
     } 
    } 

} 

extension TakePhotoViewController: AVCaptureFileOutputRecordingDelegate { 

    func capture(_ captureOutput: AVCaptureFileOutput!, didStartRecordingToOutputFileAt fileURL: URL!, fromConnections connections: [Any]!) { 


    } 

    func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) { 
     if (error != nil) { 
      print("Error recording movie: \(error!.localizedDescription)") 
     } else { 

      videoRecorded = outputURL! as URL 
      print(videoRecorded) 
      print("recorded") 
      performSegue(withIdentifier: "showVideoPreview", sender: nil) 


     } 

    } 

} 

答えて

0

このコードはここで動作しますが、私はあなたと同じ問題がありました。そして私はファイルパスの最後に "mov"ファイルパス拡張子を追加しなければならなかった。以下を参照してください:

func recordVideo() { 

    let recordingDelegate: AVCaptureFileOutputRecordingDelegate! = self 

    let videoFileOutput = AVCaptureMovieFileOutput() 
    videoOutput = videoFileOutput 
    self.captureSession.addOutput(videoFileOutput) 

    let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] 
    let filePath = documentsURL.appendingPathComponent("introVideo").appendingPathExtension("mov") 

    videoFileOutput.startRecording(toOutputFileURL: filePath, recordingDelegate: recordingDelegate) 

} 

そして私はそのURLを取り、ように私のVideoPlayerControllerにそれを渡さ:

:ここでは、その後

func capture(_ captureOutput: AVCaptureFileOutput!, didFinishRecordingToOutputFileAt outputFileURL: URL!, fromConnections connections: [Any]!, error: Error!) { 

    if error == nil { 

     let videoVC = PlayVideoController() 
     videoVC.url = outputFileURL! 
     self.navigationController?.pushViewController(videoVC, animated: true) 
    } 

    return 
} 

とは、単に記録された映像を再生するVideoPlayerControllerに私のコードです

class PlayVideoController: UIViewController { 

var url: URL! 
var player: AVPlayer! 
var avPlayerLayer: AVPlayerLayer! 

override func viewWillAppear(_ animated: Bool) { 

    if url != nil { 

     print(url) 
     player = AVPlayer(url: url) 
     let playerLayer = AVPlayerLayer(player: player) 
     self.view.layer.addSublayer(playerLayer) 
     playerLayer.frame = self.view.frame 
     player.play() 

    } 
} 

}

この情報がお役に立てば幸い!

関連する問題