2016-04-28 16 views
0

我開發了一個應用程序,其中一個功能是查看從服務器下載的視頻。我使用Alamofire訪問網絡,這是我的代碼:下載並在iPad或iPhone上播放的視頻的罕見問題

func GetVideoFiedMedia(videoFiedData: VideofiedVideo?, completionHandler: (NSURL?, NSError?) ->()) { 
    var result: NSURL? = nil; 
    let parameters : [ String : AnyObject] = [ 
     "CnxID": (videoFiedData?.cnxID!)!, 
     "TaskNum": (videoFiedData?.taskNum!)!, 
     "Ev_File": (videoFiedData?.evFile!)! 
    ] 
    let headers = [ 
     "Content-Type": "application/json" 
    ] 
    let urlAux = "https://xxxxxx/xxxxx/xxxxx.svc/VideoMedia?"; 
    Alamofire.request(.POST, urlAux, parameters: parameters, headers: headers, encoding: .JSON) 
     .validate() 
     .responseString { response in 
      switch response.result { 
      case .Success: 
       if let JSON = response.result.value { 
        do{ 
         let data: NSData = JSON.dataUsingEncoding(NSUTF8StringEncoding)! 
         let decodedJson = try NSJSONSerialization.JSONObjectWithData(data, options: .MutableLeaves) as! NSDictionary 
         let dObj = decodedJson["d"] as! NSDictionary; 
         let resultSet = dObj["Media"] as? NSArray; 
         if(resultSet != nil){ 
          let stringsData = NSMutableData(); 
          for item in resultSet! { 
           let byte = item as! Int; 
           var char = UnicodeScalar(byte); 
           stringsData.appendBytes(&char, length: 1) 
          } 
          var destinationUrl: NSURL? = nil; 
          let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! as NSURL; 
          let fileName = "msavid.mp4"; 
          destinationUrl = documentsUrl.URLByAppendingPathComponent(fileName) 
          let fileMangr = NSFileManager.defaultManager() 
          var fileHandle = NSFileHandle.init(forWritingAtPath: (destinationUrl?.path!)!) 
          if(fileHandle == nil){ 
           fileMangr.createFileAtPath((destinationUrl?.path!)!, contents: stringsData, attributes: nil) 
           fileHandle = NSFileHandle.init(forWritingAtPath: (destinationUrl?.path!)!) 
          } 
          if(fileHandle != nil){ 
           fileHandle?.seekToEndOfFile(); 
           fileHandle?.writeData(stringsData); 
           fileHandle?.closeFile(); 
          } 
          result = destinationUrl; 
          completionHandler(result, nil); 
         } 

        }catch{ 
         result = nil; 
         completionHandler(result, nil); 
        } 
       } 
      case .Failure(let error): 
       completionHandler(result, error); 
      } 
    } 
} 

當我得到了我以這種方式發揮它的視頻NSURL:

_ = self.manager.GetVideoFiedMedia(videoFiedItem, completionHandler: { responseObject, error in 
        if(responseObject != nil){ 
         var sendSegue = false; 
         self.nsurl = responseObject; 
         if NSFileManager().fileExistsAtPath(responseObject!.path!) == true { 
          if(sendSegue == false){ 
           self.performSegueWithIdentifier("sureViewSegue", sender: nil); 
           self.nsurl = nil; 
           sendSegue = true; 
           MBProgressHUD.hideAllHUDsForView(self.view, animated: true); 
          } 
         }else{ 
          MBProgressHUD.hideAllHUDsForView(self.view, animated: true) 
          let alert = UIAlertController(title: "Alert", message: "We have problem to download the media data, please try again later.", preferredStyle: UIAlertControllerStyle.Alert); 
          alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil)); 
          self.presentViewController(alert, animated: true, completion: nil); 
         } 
        }else{ 
         MBProgressHUD.hideAllHUDsForView(self.view, animated: true) 
         let alert = UIAlertController(title: "Alert", message: "We have problem to download the media data, please try again later.", preferredStyle: UIAlertControllerStyle.Alert); 
         alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default, handler: nil)); 
         self.presentViewController(alert, animated: true, completion: nil); 
        } 
       }) 

我進行推的SEGUE一個AVPlayerViewController。

當我測試使用iOS模擬器的方法時,一切似乎都正常工作,問題出現在我嘗試在真實設備(iPhone或iPad)中使用該功能時視頻未顯示出來,帶有此符號的AVPlayerViewController無法再現視頻。

請任何幫助,我無法弄清楚是什麼原因造成的問題。

回答

0

這麼簡單,同時也是不可想象的,只需重置設備並擦除手機中文件msavid.mp4的副本並且它正在工作。