如何在bundle中保存文件?

时间:2016-09-28 09:06:13

标签: swift bundle

我从url下载文件到本地。这是音频(mp3)。我正在使用Jukebox播放音频。但我无法播放本地下载的文件。我认为我需要将文件保存在捆绑中。

我正在尝试这样做,但这不起作用:

 func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
    let downloadInfo=downloadTaskList.filter({m in
        m.downloadTaskId == downloadTask.taskIdentifier
    })
    let downloadedAudio=audios[(downloadInfo.last?.row)!]
    let path=Bundle.main.resourceURL
    print(path)
    let filemanager = FileManager.default
    let documentsUrl = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    let destination = try! documentsUrl.appendingPathComponent((NSURL(string: (downloadInfo.last?.url)!)?.lastPathComponent)!)
    print(destination)
    if (!filemanager.fileExists(atPath: String(describing: destination))){
        do{
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            try FileManager.default.moveItem(at: location as URL, to:path!)
            let track=Track(id: downloadedAudio.id, title: downloadedAudio.title, desc:downloadedAudio.desc, path: String(describing: path),status: false,isDownload: false,row:(downloadInfo.last?.row)!)
            self.local.addMusic(selected: track)
            self.audios.remove(at: (downloadInfo.last?.row)!)

            let alertController = UIAlertController(title: "Успешно", message:
                "Аудио сохранен", preferredStyle: UIAlertControllerStyle.alert)
            alertController.addAction(UIAlertAction(title: "Закрыть", style: UIAlertActionStyle.default,handler: nil))
            self.present(alertController, animated: true, completion: nil)

        }catch let error as NSError{
            print(error.localizedDescription)
        }
    }

    downloadInfo.last?.isDownload=true
    let taskCell=downloadInfo.last?.cell
    taskCell?.downloadBtn.layer.isHidden=false
    taskCell!.loadingView.isHidden=true
    UIApplication.shared.isNetworkActivityIndicatorVisible = false
    taskCell!.indicator.stopAnimating()
    self.tableView.reloadData()

}

如何从包中保存文件或使用Jukebox播放本地文件?

0 个答案:

没有答案