2017-01-12 48 views
0

錄製到視頻並將其發送到服務器。無論何時服務器與我錄製的視頻,它都會返回內部服務器錯誤。但是,當我上傳一些除錄製的視頻外的其他視頻,則會成功上傳。添加下面的代碼。無法使用AVFoundation將錄製的視頻上傳到服務器 - 使用AVFoundation將Swift 3

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開始recording.Below是開始記錄的代碼。

 - (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

我使用的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) 
    } 



} 
+0

雖然轉換爲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 00 000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00>打印這個..用虛擬視頻它顯示一些其他值。 –

相關問題