ios Swift - captured video not wide

201 Views Asked by At

I am using Swift 2.3.

I am creating an app that can record and playback videos. The problem I have encountered is one in which the playback of the recorded video shows in a very narrow width (see the screen shot).

I assume the issue is related to how the video is captured.

How can I have the playback use the entire with of the view without distorting it?

I have provide 4 items for review:

  1. captureVideo(), which is used to initiate the camera for capturing the video
  2. playVideo(), which is used to play the video that was just captured. Note that self.videoAsset is set when the video is captured and used for the playback
  3. Extension to save the video
  4. videoWasSavedSuccessfully() method that is invoked when the video is saved. This method sets self.videoAsset, which is used to playback the video

1. captureVideo()
method to capture the video

private func captureVideo() {
if (UIImagePickerController.isSourceTypeAvailable(.Camera))  
  {

    if   UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil

  {

    self.howWasVideoProvided = .TakenWithCamera

    self.imagePickerController.sourceType = .Camera
    self.imagePickerController.mediaTypes = [kUTTypeMovie as String]
    self.imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureMode.Video
    self.imagePickerController.videoMaximumDuration = Globals.videoMaximumDuration

    self.presentViewController(self.imagePickerController, animated: true, completion: {})
  }
}

2. playVideo()
Method for playing the video, using self.videoAsset, which is set when the video is saved

@IBAction func playVideo(sender: UIButton)

{

  if self.videoAsset != nil
  {
    let playerItem = AVPlayerItem(asset: self.videoAsset!)

    // Play the video
    let player = AVPlayer(playerItem: playerItem)

    let playerViewController = AVPlayerViewController()
    playerViewController.player = player

    self.presentViewController(playerViewController, animated: true, completion: { () -> Void in
      playerViewController.player!.play()

    })

  }
}

3. Extension to save the video
when self.videoWasSavedSuccessfully() is invoked, it will set self.videoAsset, which is used to playback the video

extension MaintainVideoContentTableViewController:     UIImagePickerControllerDelegate, UINavigationControllerDelegate

{

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])

{

if let videoNSURL = (info[UIImagePickerControllerMediaURL] as? NSURL)
{
  let videoAsset = AVAsset(URL: videoNSURL)

  let duration = videoAsset.duration

  if round(CMTimeGetSeconds(duration)) <= Globals.videoMaximumDuration
  {
    if let relativePath = videoNSURL.relativePath
    {
      // Save video to the main photo album
      let selectorToCall = #selector(MaintainVideoContentTableViewController.videoWasSavedSuccessfully(_:didFinishSavingWithError:context:))

      self.videoNSURL = videoNSURL

      if self.howWasVideoProvided == .TakenWithCamera
      {
        UISaveVideoAtPathToSavedPhotosAlbum(relativePath, self, selectorToCall, nil)
      }

      self.imagePickerController.dismissViewControllerAnimated(true, completion: {
        // Anything you want to happen when the user saves an video
      })
    }
  }
}
}
}

4. videoWasSavedSuccessfully()
method that is invoked when the video is saved. It will set self.videoAsset, which is used to playback the video

func videoWasSavedSuccessfully(video: String, didFinishSavingWithError     error: NSError!, context: UnsafeMutablePointer<()>)


{
    if error == nil {
        if let videoNSURL = self.videoNSURL
        {
        self.videoAsset = AVAsset(URL: videoNSURL)
        }
    }
} 

image captured and replayed view phone

0

There are 0 best solutions below