平移手势时AVPlayer冻结

时间:2016-06-29 00:08:30

标签: ios swift avplayer uipangesturerecognizer

我正在开发一个用swift 2.0编写的音乐应用程序。
现在,我正在用AVPlayer实现视频播放器部分。

我想添加一个功能,如果用户向下滑动模态中的任何位置(在电影播放器​​中),模态会向下消失。(就像ios youtube播放器;它们不会实际上虽然忽略了球员。

我研究了如何实现此功能并找到了以下解决方案

Stackoverflow回答:In iOS, how to drag down to dismiss a modal?
完整教程:http://www.thorntech.com/2016/02/ios-tutorial-close-modal-dragging/

效果很好,但存在问题 AVPlayer在进行平移手势时冻结(同时在屏幕上按住手指)。音频正常播放。只有视频冻结。

以下是代码处理平移手势。

@IBAction func handleGesture(sender: UIPanGestureRecognizer) {

        let percentThreshold:CGFloat = 0.2

        // convert y-position to downward pull progress (percentage)
        let translation = sender.translationInView(view)
        let verticalMovement = translation.y / view.bounds.height
        let downwardMovement = fmaxf(Float(verticalMovement), 0.0)
        let downwardMovementPercent = fminf(downwardMovement, 1.0)
        let progress = CGFloat(downwardMovementPercent)

        guard let interactor = interactor else { return }

        switch sender.state {
        case .Began:
            interactor.hasStarted = true
            dismissViewControllerAnimated(true, completion: nil)
        case .Changed:
            interactor.shouldFinish = progress > percentThreshold
            interactor.updateInteractiveTransition(progress)
        case .Cancelled:
            interactor.hasStarted = false
            interactor.cancelInteractiveTransition()
        case .Ended:
            interactor.hasStarted = false
            if interactor.shouldFinish {
                interactor.finishInteractiveTransition()
            } else {
                interactor.cancelInteractiveTransition()
            }
        default:
            break
        }
    }

调用“dismissViewControllerAnimated(true,completion:nil)”之后(case .Began :),“currentVideoFrameRate”(avPlayer.currentItem?.tracks.first?.currentVideoFrameRate)从大约减去。 29至4

似乎内部控制了利率。我仍然不知道如何防止降低帧率 我想让AVPlayer正常播放视频,即使它正在转换(.Changed status)
有谁知道如何解决这个问题?

谢谢!

1 个答案:

答案 0 :(得分:0)

迅速5.0 在AVPlayerViewControllerDelegate方法中:

func playerViewController(_ playerViewController: AVPlayerViewController, willEndFullScreenPresentationWithAnimationCoordinator coordinator: UIViewControllerTransitionCoordinator){
        playerViewController.dismiss(animated: true, completion: nil)
    }

将解决问题。