swift - 在 iPad 或 iPhone 中下载和播放视频时出现罕见问题

标签 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带有此符号的无法重现视频。

请帮忙解决这个问题,我不知道是什么原因造成的。

最佳答案

如此简单且难以想象,只需重置设备并删除手机中 msavid.mp4 文件的副本即可正常工作。

关于swift - 在 iPad 或 iPhone 中下载和播放视频时出现罕见问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36922819/

相关文章:

swift - 如何使用alamofire发送数组参数

ios - 如何从另一个处于 segue 中的 View Controller 实例化导航和选项卡 Controller (显示详细信息)?

objective-c - NSFetchRequest 的 fetchBatchSize 导致在 Swift 中立即获取所有批处理

json - 使用值有时是 Int 有时是 String 的 codable

ios - Alamofire:图片下载请求问题

ios - Alamofire 对 Base64 字符串的响应

ios - 带有 fireDate 的通知会立即触发

json - 当我使用 swift 发布 json 时如何解析 http 代码 412 和 200?

ios - 从 SWIFT 中的 API 缓存 JSON 的最佳方法?

ios - 收集几个 Alamofire 请求的所有响应