首页 文章

以某种方式生成AVPlayerItem的副本而不是访问引用

提问于
浏览
0

我有一个使用AVQueuePlayer对象的简单音频播放列表 . 通过迭代UITableView来填充队列,该UITableView包含自定义UITableViewCell对象PlayableAudioCell . 这些对象有一个AVPlayerItem属性audioTrack,我将其插入到AVQueueplayer的队列中 .

我想在播放和完成项目时向这些曲目添加和删除AVPlayerItemDidPlayToEndTime观察者,以便我可以更新我创建的音乐播放器的标签 .

出于某种原因,如果队列长于2个轨道,则队列中的第三个轨道不会将观察者添加到其中,因为应用它的函数会传递轨道的副本,而不是已保存的轨道到PlayableAudioCell对象 .

我无法理解这是怎么回事,但我必须在某处创建一个轨道副本,而不是引用单元格的音轨 . 我可以从音轨的内存位置的打印语句中看出来 .

只是对我的应用程序布局的一些解释...它包含一个tableViewController视图,允许用户选择一个单元格并播放一个轨道 . 我还有一个SplitViewController,允许FooterView弹出并显示播放曲目信息(标签,持续时间,曲目滑块) .

操作顺序:

  • 用户按下TableView中单元格的播放

  • AudioQueuePlayer的Queue由按下的轨道和它下面的每个轨道填充

  • TableViewController的委托(FooterViewController)调用它的委托播放方法

  • AVPlayerItemDidPlayToEndTime观察者被添加到播放的第一个项目

  • 当曲目结束时,从观察者调用选择器 . 在此选择器中,删除当前轨道的观察者,并添加将AVPlayerItemDidPlayToEndTime观察者添加到下一轨道的代码 .

当我访问一个单元格或一个audiotrack的副本而不是引用currentSelectedAudioCell的audiotrack属性时,是否有任何此代码弹出?

我将在TableViewController中省略我的代码...我可以告诉它在表中正确填充,问题必须在FooterViewController的代码中:

func play(cell: PlayableAudioCell){

    if let previousCell = currentlySelectedAudioCell, let prevFilename = currentlySelectedAudioCell?.chapter?.value(forKey: "Filename") as? String, let filename = cell.chapter?.value(forKey: "Filename") as? String{

        if filename != prevFilename{
            runningTime = 0
        }
    }

    updateWithObserver(cell: cell)

    //show footerview if it is hidden.
    if(footerView.superview?.isHidden)!{
        hideShowFooterView()
    }

    if(self.isPlaying)!{
        //print("Should pause cell")
        audioQueuePlayer?.pause()
        switchState(state: "Pause")

        playPauseRunTime(state: "Pause")

    }else{
        //print("Should play cell")
        audioQueuePlayer?.play()
        switchState(state: "Play")

        playPauseRunTime(state: "Play")
    }

    updateFooterView()
}

func updateWithObserver(cell: PlayableAudioCell){

    self.currentlySelectedAudioCell = cell
    delegate?.currentlySelectedAudioCell = cell

    let track = cell.audioTrack!

    NotificationCenter.default.addObserver(self, selector: #selector(FooterViewController.playerItemDidReachEnd(notification:)), name: Notification.Name.AVPlayerItemDidPlayToEndTime, object: track)

}

func playerItemDidReachEnd(notification: Notification){
    NotificationCenter.default.removeObserver(self, name: notification.name, object: audioQueuePlayer?.currentItem)

    if didAudioPlayEntirely(){
        recordAudioHistoryToFirebase(didFinish: true)
        if let currentlySelectedAudioCell = delegate?.currentlySelectedAudioCell{
            revealCheckMark(cell: currentlySelectedAudioCell)
        }
    }

    runningTime = 0

    //If there is just one more item in the queue, regardless of whether playAll() has been toggled...
    if (audioQueuePlayer?.items().count)! <= 1 {

        playPauseRunTime(state: "Pause")

        audioQueuePlayer?.removeAllItems()
        resetFooterViewUI()
        switchState(state: "Pause")
        hideShowFooterView()

        audioQueuePlayer?.rate = 0.0

        delegate?.currentlySelectedAudioCell = nil
        currentlySelectedAudioCell = nil
        delegate?.indexPathOfSelectedCell = nil
        indexPathOfSelectedCell = nil

    }else{

        if let indexPathOfSelectedCell = indexPathOfSelectedCell{

            playPauseRunTime(state: "Pause")
            playPauseRunTime(state: "Play")

            let row = indexPathOfSelectedCell.row + 1
            let newIndexPath = IndexPath(row: row, section: 0)
            self.indexPathOfSelectedCell = newIndexPath
            delegate?.indexPathOfSelectedCell = newIndexPath

            let newCell = self.tableView!.cellForRow(at: newIndexPath) as? PlayableAudioCell
            updateWithObserver(cell: newCell!)

            updateFooterView()
            self.tableView!.reloadData()

        }
    }
}

1 回答

  • 0

    显然,AVQueuePlayer每次完成其队列中的项目播放时都会删除AVPlayerItem . 我的逻辑是尝试将队列的索引与tableview单元格的数组进行匹配,经过几次迭代后,索引将关闭,并且观察者不会被添加到正确的播放项目中 . 通过用AVPlayer和AVPlayerItem数组替换AVQueuePlayer来修复 .

相关问题