使用AVPlayer时音频和视频播放不一致

时间:2019-05-14 14:43:38

标签: ios swift audio avplayer avplayerviewcontroller

我试图通过点击UICollectionViewCell中的CollectionViewCell来播放远程URL中的特定视频。这些URL与该特定UICollectionViewCell

相关联

有时会同时播放视频和音频,但随后,在点按时会播放视频,但根本听不到音频。我只有戴上耳机听才能听到声音。

我希望在敲击任何CollectionViewCell cell时都能听到声音和播放视频,即始终如一地,每次敲击都可以听到视频和音频。如何解决上述问题并确保这方面的一致性?

这是我与UICollectionViewCell相关的代码段:

    func playVideo(videoSource: String) {
        if URL(string: videoSource) != nil {
            let player = AVPlayer(url: URL(string: videoSource)!)

            let playerViewController = AVPlayerViewController()
            playerViewController.player = player

            let state = UIApplication.shared.applicationState
            do {
                self.chiefView!.present(playerViewController, animated: true) {
                    if state == .background  || state == .inactive {
                        playerViewController.player!.pause()
                    } else if state == .active {
                        playerViewController.player!.play()
                    }
                }
            } catch {
                print(error)
            }
        }
    }

    func configure(_ parentView: UIViewController, cellPost: Post?, type: Category) {
        linkString = "https://thesportsrush.com/wp-content/uploads/2019/04/GOPAL.png"
        chiefView = parentView
        post = cellPost
        category = type

        postTypeView.image = UIImage(named: "Glass")
        if post == nil {
            hotView.KFDownload(from: linkString)
            if hotView.image == nil {
                hotView.image = UIImage(named: "sportsContributeBlack")
            }
            self.label.text = "This is the hottest topic. It is trending right now."
        } else {
            if category == .General || category == .Article {
                hotView.KFDownload(from: post!.mediaLibraries.first!.src)
                postTypeView.image = UIImage(named: "Glass")
                linkString = post!.mediaLibraries.first!.src
            } else if category == .Video {
                hotView.KFDownload(from: post!.videoThum)
                linkString = post!.videoThum
                postTypeView.image = UIImage(named: "Video")
            }
            if hotView.image == nil {
                hotView.image = UIImage(named: "Sportsflashes")
            }
            self.label.text = post!.title
        }
        self.label.font = UIFont(name: "IBMPlexSans-Bold", size: 15)

        self.durationLabel.text = "6 mins read"
        self.durationLabel.font = UIFont(name: "IBMPlexSans", size: 13)
    }

在单击UICollectionViewCell中的特定CollectionViewCell时执行playVideo功能。

    self.isUserInteractionEnabled = true
    let landingPageTap = UITapGestureRecognizer(target: self, action: #selector(self.hotViewTap))
    self.addGestureRecognizer(landingPageTap)

    @objc func hotViewTap(sender: UIGestureRecognizer) {
        //non relevant code
        self.playVideo(videoSource: self.post!.mediaLibraries[0].src)
        //non relevant code
    }

这是我的viewcontroller代码,其中有多个UICollectionViews,这些代码具有上述各行中的单元格:

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if collectionView == self.hotCollectionView {
            return self.videos.count
        }
        else if collectionView == self.videoOneCollectionView {
            return self.highlightOneRows.count
        }
        else if collectionView == self.videoTwoCollectionView {
            return self.highlightTwoRows.count
        }
        else if collectionView == self.engageCollectionView {
            return self.engageRows.count
        }
        return 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if collectionView == self.hotCollectionView {
            let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HotCell", for: indexPath as IndexPath) as! HotViewCell
            myCell.configure(self, cellPost: videos[indexPath.row], type: HotViewCell.Category.Video)
            myCell.layer.borderColor = UIColor.lightGray.cgColor
            myCell.layer.cornerRadius = 15
            myCell.layer.borderWidth = 0.35
            myCell.layer.masksToBounds = true
            return myCell
        }
        else if collectionView == self.videoOneCollectionView {
            let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "StoryCellOne", for: indexPath as IndexPath) as! StoryViewCell
            myCell.configure(self, cellPost: highlightOneRows[indexPath.row], type: StoryViewCell.Category.Video)
            myCell.layer.borderColor = UIColor.lightGray.cgColor
            myCell.layer.cornerRadius = 15
            myCell.layer.borderWidth = 0.35
            myCell.layer.masksToBounds = true
            return myCell
        }
        else if collectionView == self.videoTwoCollectionView {
            let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "StoryCellTwo", for: indexPath as IndexPath) as! StoryViewCell
            myCell.configure(self, cellPost: highlightTwoRows[indexPath.row], type: StoryViewCell.Category.Video)
            myCell.layer.borderColor = UIColor.lightGray.cgColor
            myCell.layer.cornerRadius = 15
            myCell.layer.borderWidth = 0.35
            myCell.layer.masksToBounds = true
            return myCell
        }
        else if collectionView == self.engageCollectionView {
            let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "EngageCell", for: indexPath as IndexPath) as! EngageViewCell
            myCell.configure(self.engageRows[indexPath.row])
            myCell.layer.borderColor = UIColor.black.cgColor
            myCell.layer.cornerRadius = 7.5
            myCell.layer.borderWidth = 0.5
            myCell.layer.masksToBounds = true
            return myCell
        }
        return UICollectionViewCell()
    }

所讨论的单元格为HotViewCellStoryViewCell,它们具有相似的代码。

playVideo()函数的do-catch块中,我还使用了以下所示的不同语句,但即使这样似乎也无济于事。

AVAudioSession.sharedInstance().setCategory()
AVAudioSession.sharedInstance().setActive()

如果audio不起作用,那么我注释以上各行,然后在遇到问题时再次取消注释,并添加其他参数。这已经一次又一次地重复。

我什至考虑使用此链接中的代码: Relevant problem

但是首先,我想看看我设置当前代码的方式是否有问题。

我在这里想念什么?我不确定,但是也许我在playVideo()函数中做错了什么?任何帮助表示赞赏。

0 个答案:

没有答案