2016-10-29 7 views
0

Я пытаюсь загрузить mp3-файлы в iPhone со скоростью. Но эти файлы SAMETIMES повреждены. (Из-за этой проблемы меняется время воспроизведения загруженного Mp3, поэтому он не может прочитать правильное время продолжительности в MPmovieplayer). Я не знаю, почему. Можете ли вы помочь мне решить проблемы?Загруженный файл, MP3-файл, поврежден через сеанс NSURL в Swift

func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse) 
{ 
    self.totalByte = self.totalByte + Double(response.expectedContentLength) 
    self.delegate?.onDownLoadStart?(self.fileId,filePath:self.savePath, fileLength: self.totalByte,loadPath:self.filePath, info:self.info!) 

} 

func connection(connection: NSURLConnection, didReceiveData data: NSData) 
{ 
    if(self.isSaved){ 

     if(self.bigFile == nil){ 
      if(self.file != nil){ 
       self.file!.appendData(data) 
      } 

      self.bigFile = NSFileHandle(forUpdatingAtPath: self.savePath) 
      self.file = nil 
     }else{ 
      self.bigFile!.seekToEndOfFile() 
      self.bigFile!.writeData(data) 

     } 
     self.progressByte = progressByte + Double(data.length) 

    }else{ 
     if(self.file == nil){ 
      self.file = NSMutableData() 
     } 
     self.file!.appendData(data) 
     progressByte = Double(file!.length) 
    } 

    self.delegate?.onDownLoadProgress?(self.fileId, progressPct:Float(self.progressByte/self.totalByte)) 


} 


func connectionDidFinishLoading(connection: NSURLConnection){ 
    self.delegate?.onDownLoadComplete(self.fileId, filePath:self.savePath, fileData:self.file,loadPath:self.filePath, info: self.info!) 
self.removeFileHandle() 
} 


private func removeFileHandle() 
{ 
    bigFile?.closeFile() 
    bigFile=nil; 
    file=nil; 
} 


func connection(connection: NSURLConnection, didFailWithError error: NSError){ 

    print("DownLoader path="+self.filePath+" errorCode="+error.code.description); 
    self.removeFileHandle() 

    self.delegate?.onDownLoadError?(self.fileId,filePath:self.savePath, withError:error) 


} 

ответ

Смежные вопросы