无法使用AVFoundation将录制的视频上传到服务器 - Swift 3

时间:2017-01-12 11:19:50

标签: objective-c swift3 avfoundation nsfilemanager avkit

我正在使用AVFoundation录制视频并将其发送到服务器。每当服务器上有我录制的视频时,它都会返回内部服务器错误。但是当我上传除录制的视频以外的其他虚拟视频时,它会成功上传。添加以下代码。

let documentsPath = NSURL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])
        let logsPath = documentsPath.appendingPathComponent(GlobalVar.interVCode)


    if !FileManager.default.fileExists(atPath: (logsPath?.absoluteString)!){
    do {
        try FileManager.default.createDirectory(at: logsPath!, withIntermediateDirectories: true, attributes: nil)
                   } catch let error as NSError {
        NSLog("Unable to create directory \(error.debugDescription)")
    }
    }
     let outputURL: URL = (self.applicationDocumentsDirectory().appendingPathComponent(GlobalVar.interVCode)?.appendingPathComponent("\(questionId)").appendingPathExtension("mp4"))!


 self.camera.startRecording(withOutputUrl: outputURL, didRecord: 

{(camera, outputURL, error) in

    })

使用自定义相机控制器LLSimpleCamera开始录制。下面是开始录制的代码。

     - (void)startRecordingWithOutputUrl:(NSURL *)url didRecord:(void (^)(LLSimpleCamera *camera, NSURL *outputFileUrl, NSError *error))completionBlock
{
    // check if video is enabled
     if(!self.videoEnabled) {
        NSError *error = [NSError errorWithDomain:LLSimpleCameraErrorDomain
                                                 code:LLSimpleCameraErrorCodeVideoNotEnabled
                                         userInfo:nil];
        [self passError:error];
        return;
    }

    if(self.flash == LLCameraFlashOn) {
        [self enableTorch:YES];
    }

    // set video orientation
    for(AVCaptureConnection *connection in [self.movieFileOutput connections]) {
        for (AVCaptureInputPort *port in [connection inputPorts]) {
            // get only the video media types
            if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
                if ([connection isVideoOrientationSupported]) {
                    [connection setVideoOrientation:[self orientationForConnection]];
                }
            }
        }
    }

    self.didRecordCompletionBlock = completionBlock;

    [self.movieFileOutput startRecordingToOutputFileURL:url recordingDelegate:self];
}

非常感谢。

1 个答案:

答案 0 :(得分:1)

我正在使用UIImagePickerController进行录制和上传到服务器

    if   UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera){

    imagePicker.delegate = self
    imagePicker.sourceType = .camera
    imagePicker.mediaTypes = [kUTTypeMovie as NSString as String]
    imagePicker.allowsEditing = false
    imagePicker.videoMaximumDuration = TimeInterval(10.0)

    self.present(imagePicker, animated: true, completion: nil)
    }

 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
    picker .dismiss(animated: true, completion: nil)



    let mediaType = info[UIImagePickerControllerMediaType] as! NSString

    if mediaType == kUTTypeMovie {
        guard let path = (info[UIImagePickerControllerMediaURL] as! NSURL).path else { return }
        if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path) {
 if let fileURL = info[UIImagePickerControllerMediaURL] as? NSURL {
                if let videoData = NSData(contentsOf: fileURL as URL) {
                    print(videoData.length)

                    let fileManager = FileManager.default
                    let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("xyz.mov")
                    fileManager.createFile(atPath: paths as String, contents: videoData as Data, attributes: nil)
                }
            }


        }
    }else{
  }

 }

使用方法从文档目录中获取视频数据并上传到服务器

func uploadVideo(_ userDetails : [String : AnyObject]) {

    let imagePAth = URL(fileURLWithPath:(self.getDirectoryPath() as NSString).appendingPathComponent("xyz.mov") )

    //let videoData = UIImage(contentsOfFile: imagePAth)!

    do {let data = try Data(contentsOf: imagePAth)

        print(data)
 } catch {
        print(error)
    }



}