【问题标题】:Not able to upload recorded video to server using AVFoundation - Swift 3无法使用 AVFoundation 将录制的视频上传到服务器 - Swift 3
【发布时间】:2017-01-12 11:19:49
【问题描述】:

我正在使用 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];
}

非常感谢您的帮助。

【问题讨论】:

    标签: objective-c swift3 avfoundation nsfilemanager avkit


    【解决方案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)
        }
    
    
    
    }
    

    【讨论】:

    • 虽然转换为NSData的00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00>打印该..与虚拟视频它表示一些其它值。跨度>
    猜你喜欢
    • 2016-05-15
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 2013-11-25
    • 2021-07-07
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    相关资源
    最近更新 更多