在iPad或iPhone上下载和播放视频的罕见问题

时间:2016-04-28 18:33:16

标签: swift alamofire

我开发了一个应用程序,其中一个功能是查看我从服务器下载的视频。我正在使用Alamofire访问网络,这是我的代码:

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使用此符号无法重现视频。

请对此提出任何帮助,我无法弄清楚导致问题的原因。

1 个答案:

答案 0 :(得分:0)

如此简单,同时也是不可想象的,只需重置设备并删除手机中文件msavid.mp4的副本即可。