我试图(1)从链接下载一段音频,(2)将新下载的音频添加到AVPlayer并(3)播放它。在步骤(3)出现问题,我正在寻找任何指导。这是代码,包括我的alamofire和下载功能,因为我担心在那个阶段可能会出现问题。
import AVFoundation
class SettingAlarmViewController: UIViewController {
var player:AVPlayer!
override func viewDidLoad() {
super.viewDidLoad()
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
}
catch {
// report for an error
}
}
func getLatestPodcastURL(completion: @escaping (URL) -> ()) {
let RSSUrl: String = "https://www.npr.org/rss/podcast.php?id=510318"
Alamofire.request(RSSUrl).responseRSS() {response in
if let podcastURL: String = response.result.value?.items[0].enclosure!
{
let audioURL = URL(string: podcastURL)
completion(audioURL!)
}
else {
//error handling
}
}
}
func downloadSongAsynch(audioUrl: URL, completion: @escaping (URL) -> ()) {
let fileManager = FileManager.default
let documentsDirectoryURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("podcasts/")
do {
try fileManager.createDirectory(atPath: documentsDirectoryURL.path,
withIntermediateDirectories: true, attributes: nil)
} catch {
//error handling
}
let destinationUrl = documentsDirectoryURL.appendingPathComponent(audioUrl.lastPathComponent)
URLSession.shared.downloadTask(with: audioUrl, completionHandler: { (location, response, error) -> Void in
guard let location = location, error == nil else { return }
do {
try FileManager.default.moveItem(at: location, to: destinationUrl)
} catch {
//error handling
}
}).resume()
completion(destinationUrl)
}
@IBAction func SetUpBotton(_ sender: Any) {
getLatestPodcastURL() {response in
//Uses an asynchronous call to Alamofire to get the podcast URL
self.downloadPodcastAsynch(audioUrl: response){response2 in
self.player = AVPlayer(url: response2)
print(self.player.currentItem)
}
@IBAction func PlayButton(_ sender: Any) {
player.play()
print(player.currentItem)
}
日志始终显示我当前的项目:>
但没有任何戏剧。我已经通过尝试使用URL来传输此内容来检查音频是否正常工作。这很好。我得到以下内容:
BoringSSL错误" [BoringSSL]函数boringssl_session_errorlog:第2871行[boringssl_session_read] SSL_ERROR_ZERO_RETURN(6):操作失败,因为连接已通过close_notify警报干净地关闭
但是从我所读过的内容来看,这只是最新更新中的一个错误,不应该影响下载。有什么想法吗?