我是Swift的新手,因此不是很有经验。我不知道为什么这不起作用。
我正在尝试下载音乐文件,然后将其发送到AVAudoPlayer进行播放。
以下是代码:
@IBAction func startDownload(_ sender: Any) {
weak var weakSelf = self
let url = URL(string: "http://www.noiseaddicts.com/samples_1w72b820/280.mp3")!
let task = DownloadManager.shared.activate().downloadTask(with: url as URL, completionHandler: { (URL, response, error) -> Void in
print("URL = \(URL)")
weakSelf!.plays(url: URL! as URL)
})
task.resume()
}
我得到的错误是:
DownloadTaskExample [31140:1527666] ***由于未捕获的异常终止应用程序' NSGenericException',原因:'后台会话不支持完成处理程序块。请改为使用代理。'
***第一次抛出调用堆栈: ( 0 CoreFoundation 0x0000000110bbf34b exceptionPreprocess + 171 1 libobjc.A.dylib 0x000000010db2f21e objc_exception_throw + 48 2 CFNetwork 0x00000001111ada2c - [__ NSURLBackgroundSession validateSerializabilityForRequest:completion:] + 172 3 CFNetwork 0x00000001111b035c - [__ NSURLBackgroundSession _onqueue_downloadTaskForRequest:resumeData:completion:] + 36 4 CFNetwork 0x00000001111af37c __90 - [__ NSURLBackgroundSession downloadTaskForRequest:downloadFilePath:resumeData:completion:] _ block_invoke + 38 5 CFNetwork 0x00000001111adddb __68 - [__ NSURLBackgroundSession performBlockOnQueueAndRethrowExceptions:] _ block_invoke + 67 6 libdispatch.dylib 0x0000000111aa00cd _dispatch_client_callout + 8 7 libdispatch.dylib 0x0000000111a7d30a _dispatch_barrier_sync_f_invoke + 340 8 CFNetwork 0x00000001111add44 - [__ NSURLBackgroundSession performBlockOnQueueAndRethrowExceptions:] + 174 9 CFNetwork 0x00000001111af2e5 - [__ NSURLBackgroundSession downloadTaskForRequest:downloadFilePath:resumeData:completion:] + 243 10 DownloadTaskExample 0x000000010d54086d _TFC19DownloadTaskExample14ViewController13startDownloadfP_T_ + 525 11 DownloadTaskExample 0x000000010d540e33 _TToFC19DownloadTaskExample14ViewController13startDownloadfP_T_ + 67 12 UIKit 0x000000010e3685b8 - [UIApplication sendAction:to:from:forEvent:] + 83 13 UIKit 0x000000010e4ededd - [UIControl sendAction:to:forEvent:] + 67 14 UIKit 0x000000010e4ee1f6 - [UIControl _sendActionsForEvents:withEvent:] + 444 15 UIKit 0x000000010e4ed0f2 - [UIControl touchesEnded:withEvent:] + 668 16 UIKit 0x000000010e3d5ce1 - [UIWindow _sendTouchesForEvent:] + 2747 17 UIKit 0x000000010e3d73cf - [UIWindow sendEvent:] + 4011 18 UIKit 0x000000010e38463f - [UIApplication sendEvent:] + 371 19 UIKit 0x000000010eb7671d __dispatchPreprocessedEventFromEventQueue + 3248 20 UIKit 0x000000010eb6f3c7 __handleEventQueue + 4879 21 CoreFoundation 0x0000000110b64311 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17 22 CoreFoundation 0x0000000110b4959c __CFRunLoopDoSources0 + 556 23 CoreFoundation 0x0000000110b48a86 __CFRunLoopRun + 918 24 CoreFoundation 0x0000000110b48494 CFRunLoopRunSpecific + 420 25 GraphicsServices 0x000000011444da6f GSEventRunModal + 161 26 UIKit 0x000000010e366964 UIApplicationMain + 159 27 DownloadTaskExample 0x000000010d54597f main + 111 28 libdyld.dylib 0x0000000111aec68d start + 1 29 ??? 0x0000000000000001 0x0 + 1 )
libc ++ abi.dylib:以NSException类型的未捕获异常终止
请有人帮助我,以便下载文件后会将其传递给播放功能,以便我可以播放:)
P.s这是我正在使用的DownloadManager:https://www.ralfebert.de/snippets/ios/urlsession-background-downloads/
答案 0 :(得分:6)
这告诉你的是
URLSession
时,您使用了后台配置对象,即使用background(withIdentifier:)
上的URLSessionConfiguration
方法。这是不允许的原因是,通过后台下载,您的应用可能无法在下载完成时运行,但iOS想要唤醒您的应用,告诉它下载已完成。一旦您的应用停止运行,该完成功能块就不会存在,因此它不是获取该通知的有效方式。
您有几个选择:
URLSessionConfiguration.default
这样的东西。但是没有后台下载。URLSessionDelegate
中声明的方法来获取下载结果。