我正在尝试为AVQueuePlayer做一个简单的设置 . 我的应用程序有两个按钮 . 一个按钮通过RSS源标识播客的URL,将其下载到应用程序上的/ Documents文件夹,并使用文件管理器中播客的新地址创建AVPlayerItem . 最后,AVPlayerItem被添加到AVQueuePlayer(它被声明为实例变量,因此它不会超出范围) .

第二个按钮告诉AVQueuePlayer播放 .

按下第一个按钮后,AVPlayerItem列在AVQueuePlayer的队列中 . 但是,当我使用第二个按钮按下播放时,没有任何反应 . 当我记录AVQueuePlayer的内容时,突然间什么都没有出现 .

如果我第二次添加AVPlayerItem,之前下载后,AVQueuePlayer工作正常 . 但是,AVPlayerItem在第一次添加时从不播放 . 是关于我如何下载文件的?关于初始化AVQueuePlayer的事情?

关于我失踪的猜测?导入AVFoundation

class SettingAlarmViewController: UIViewController {

var qPlayer:AVQueuePlayer!

override func viewDidLoad() {
    super.viewDidLoad()
    qPlayer = AVQueuePlayer()
    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
                //uses an asynchronous call to download the podcast 
                print("This should be an empty list")
                print(self.qPlayer.items())
                self.qPlayer.insert(AVPlayerItem(url:response2), after: nil)
                print("This list should have one AVPlayerItem in it")
                print(self.qPlayer.items())
            }

@IBAction func PlayButton(_ sender: Any) {
    qPlayer.play()
    print("When the PlayButton was pressed, the list looked like this")
    print(qPlayer.items())
}

这会生成以下日志:

This should be an empty list
     [ ]
     This list should have one AVPlayerItem in it
     [<AVPlayerItem: 0x1019f0fd0, asset = <AVURLAsset: 0x1019eec10, URL = file:///var/mobile/Containers/Data/Application/28F050BC....38B0F/Documents/podcast/testNPRpodcast.mp3>>]
     When the PlayButton was pressed, the list looked like this
     [ ]