• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - AVPlayer 类的实例已被释放,而键值观察者仍向其注册

[复制链接]
菜鸟教程小白 发表于 2022-12-11 18:16:54 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

以下代码在为另一个 View Controller 推送 Controller 时崩溃。获取关注错误。

***** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'An instance 0x600000009690 of class AVPlayer was deallocated while key value observers were still registered with it. Current observation info: ( Context: 0x0, Property: 0x600000446ea0> )'**

//MARK: tableViewMethods

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
    return arrVideoListing.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    //print("cell for row")
    cell = tableView.dequeueReusableCell(withIdentifier: "DashboardVideoListingCell", for: indexPath) as! DashboardVideoListingCell

    cell.lblUserName.text = (arrVideoListing.object(at: indexPath.row)as AnyObject).value(forKey: "user_name") as? String
    let videoName = (arrVideoListing.object(at: indexPath.row)as AnyObject).value(forKey: "video_title") as? String
    cell.lblHashTag.text = self.replaceChar(videoName!)

    cell.lblAddedTime.text = (arrVideoListing.object(at: indexPath.row)as AnyObject).value(forKey: "datetime") as? String
    cell.lblCategory.text = (arrVideoListing.object(at: indexPath.row)as AnyObject).value(forKey: "category") as? String

    cell.lblVotes.text = (arrVideoListing.object(at: indexPath.row)as AnyObject).value(forKey: "votes") as! String

    cell.lblDislikes.text = (arrVideoListing.object(at: indexPath.row)as AnyObject).value(forKey: "negative_votes") as! String

    cell.lblViews.text = "\((arrVideoListing.object(at: indexPath.row)as AnyObject).value(forKey: "views") as! String) views"

    cell.btnFullScreen.tag = indexPath.row
    cell.btnFullScreen.addTarget(self, action: #selector(fullScreenBtnClicked(_), for: .touchUpInside)


    cell.btnPlayPause.isHidden = true
    cell.imgPlay.isHidden = true


    if (selectedRow1 !=  "")
    {
        if selectedRow1 == "\(indexPath.row)" && isBtnShow == true
        {

            cell.btnPlayPause.isHidden = false
            cell.imgPlay.isHidden = false

        }
    }

    var temp: Int = getVisibleIndex()
    if temp == indexPath.row && fullvisible
    {
        cell.imgC.frame = CGRect(x: cell.playerView.frame.origin.x+5, y: cell.playerView.center.y+135, width: 20, height: 20)
        cell.imgC.image = UIImage (named: "C")

        cell.imgUserProfile.image = nil
        let imgurl1 = ((arrVideoListing.object(at: indexPath.row)as AnyObject).value(forKey: "user_data")as AnyObject).value(forKey: "profile_picture")as? String
        if imgurl1 == ""
        {
            cell.imgUserProfile.image = UIImage (named: "no_user_1.png")
        }
        else
        {
            let url1 = NSURL(string:imgurl1!)
            cell.imgUserProfile.imageURL = url1 as URL?
        }

            cell.btnUserInfo.tag = indexPath.row
            cell.btnUserInfo.addTarget(self, action: #selector(getUserInfoFromBtnClicked(_), for: .touchUpInside)

            let videoURL = NSURL(string: ((arrVideoListing.object(at:indexPath.row) as AnyObject).value(forKey: "url") as AnyObject) as! String)
            print(videoURL!)
            cell.playerView.isHidden = false



            player = AVPlayer(url: videoURL! as URL)

            let playerLayer = AVPlayerLayer(player: player)
            playerLayer.frame = (cell.playerView.bounds)
            playerLayer.backgroundColor = UIColor.black.cgColor
            cell.layer.addSublayer(playerLayer)
            cell.playerView?.layer.addSublayer(playerLayer)
            cell.playerView.addSubview(cell.imgC)

            activityView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
            activityView.center = cell.playerView.center
            activityView.startAnimating()
            cell.playerView.addSubview(activityView)
            addedObserver = true


            player.addObserver(self, forKeyPath: "status", options:[], context: nil)


            cell.selectionStyle = .none
            NotificationCenter.default.addObserver(self, selector: #selector(self.itemDidFinishPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)

        }
        else
        {
            cell.playerView.isHidden = true
            cell.imgThumbnail.image = nil
            let imgurl = (arrVideoListing.object(at: indexPath.row)as AnyObject).value(forKey: "thumbnail")as? String
            let url = NSURL(string:imgurl!)
            cell.imgThumbnail.imageURL = url as URL?

            cell.imgUserProfile.image = nil
            let imgurl1 = ((arrVideoListing.object(at: indexPath.row)as AnyObject).value(forKey: "user_data")as AnyObject).value(forKey: "profile_picture")as? String
            if imgurl1 == ""
            {
                cell.imgUserProfile.image = UIImage (named: "no_user_1.png")
            }
            else
            {
                let url1 = NSURL(string:imgurl1!)
                cell.imgUserProfile.imageURL = url1 as URL?
            }

            cell.btnPlayPause.tag = indexPath.row
            cell.btnPlayPause.addTarget(self, action: #selector(playVideoFromBtnClicked(_), for: .touchUpInside)

            cell.btnUserInfo.tag = indexPath.row
            cell.btnUserInfo.addTarget(self, action: #selector(getUserInfoFromBtnClicked(_), for: .touchUpInside)

            cell.selectionStyle = .none
            if player != nil
            {
                player.pause()
            }
            cell.backgroundColor = UIColor .blue

            return cell
       }
    return cell
}

func getVisibleIndex() -> Int
{
    for indexPath: IndexPath in tblVideoListing.indexPathsForVisibleRows!
    {
        let cellRect: CGRect = tblVideoListing.rectForRow(at: indexPath)
        let isVisible: Bool = tblVideoListing.bounds.contains(cellRect)
        if isVisible
        {
            index = Int(indexPath.row)
        }
    }
    return index
}

func scrollViewDidScroll(_ aScrollView: UIScrollView)
{

    let cells: [DashboardVideoListingCell] = tblVideoListing.visibleCells as! [DashboardVideoListingCell]
    var  a = 0
    for cell: DashboardVideoListingCell in cells
    {
        let path: IndexPath? = IndexPath(row: a, section: 0)
        index = Int((path?.row)!)
        fullvisible = true
        a  = a + 1
    }

}


 func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)
 {

    do {

        tblVideoListing.reloadData()

    }
    catch
    {
        print(error.localizedDescription)
    }


}

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
{

    if (keyPath == "status") {

        if player.status == .readyToPlay {

            player.play()
            if player.rate>=0
            {
                addedObserver = false


                if(player != nil && player.currentItem != nil)
                {

                    player.removeObserver(self, forKeyPath: "status", context: nil)
                }


                activityView.removeFromSuperview()

            }
        }
        else if player.status == .failed {

        }
    }
}


deinit
{
    if addedObserver == true
    {
        if(player != nil && player.currentItem != nil)
        {
            player.removeObserver(self, forKeyPath: "status", context: nil)

        }
    }
}

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath)
{
    if player != nil
    {
        player.pause()
    }
}



Best Answer-推荐答案


为每个 cellForRowAt 创建一个新播放器,为什么?

崩溃的原因大概是这行:
player = AVPlayer(url: videoURL! as URL) 如果 player 已经存在,那么它将被释放 - 如果它有观察者(它有),它将产生崩溃。

两个建议:
1. 不要在 cellForRowAt 中创建新播放器,为您的 View Controller 创建一个全局播放器(推荐)

  1. 记得在创建新玩家之前移除观察者

    player.removeObserver(self, forKeyPath: "status", context: nil)
    player = AVPlayer(url: videoURL! as URL)

关于ios - AVPlayer 类的实例已被释放,而键值观察者仍向其注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45078417/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap