2017-07-18 47 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() 

    } 
} 

}

希望這有助於!