如何在Swift 3中为AVPlayer添加滑动手势

4

我正在使用Swift 3,并且想要为AVPlayer添加滑动手势。有人告诉我,为了实现这一点,我必须使用另一个视图并将该视图置于视频的顶部 - 因此我这样做了,这是我的代码:(但没有起作用):

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController , UIAlertViewDelegate {

let myFirstButton = UIButton()
let mySecondButton = UIButton()
var scoreLabel = UILabel()
var Player = AVPlayer()
var swipeGesture = UIGestureRecognizer()
var sView = UIView()


override func viewDidLoad() {
    super.viewDidLoad()


    ////////////
    sView.frame = self.view.frame
    self.view.addSubview(sView)
    self.view.bringSubview(toFront: sView)


    //////Swipe Gesture

    let swipeRight = UISwipeGestureRecognizer(target: sView, action: #selector(self.respondToSwipeGesture))
    swipeRight.direction = UISwipeGestureRecognizerDirection.right
    self.sView.addGestureRecognizer(swipeRight)

    let swipeLeft = UISwipeGestureRecognizer(target: sView, action: #selector(self.respondToSwipeGesture))
    swipeLeft.direction = UISwipeGestureRecognizerDirection.left
    self.sView.addGestureRecognizer(swipeLeft)

    let swipeUp = UISwipeGestureRecognizer(target: sView, action: #selector(self.respondToSwipeGesture))
    swipeUp.direction = UISwipeGestureRecognizerDirection.up
    self.sView.addGestureRecognizer(swipeUp)

    let swipeCustom = UISwipeGestureRecognizer(target: sView, action: #selector(self.respondToSwipeGesture))
    swipeCustom.direction = UISwipeGestureRecognizerDirection.init(rawValue: 200)
    self.sView.addGestureRecognizer(swipeCustom)


    let swipeDown = UISwipeGestureRecognizer(target: sView, action: #selector(self.respondToSwipeGesture))
    swipeDown.direction = UISwipeGestureRecognizerDirection.down
    self.sView.addGestureRecognizer(swipeDown)


    //////////////////////End Swipe Gesture

    let currentPlayerItem = Player.currentItem
    let duration = currentPlayerItem?.asset.duration
    let currentTime = Float(self.Player.currentTime().value)


        if currentTime >= 5 {

            print("OK")

        }else if currentTime <= 5 {

            print("NO")
        }

    NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.Player.currentItem, queue: nil, using: { (_) in
        DispatchQueue.main.async {
            self.Player.seek(to: kCMTimeZero)
            self.Player.play()
        }
    })



    /////////////
    NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.Player.currentItem, queue: nil, using: { (_) in
        DispatchQueue.main.async {
            self.Player.seek(to: kCMTimeZero)
            self.Player.play()
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5.0) {
                // check if player is still playing
                if self.Player.rate != 0 {
                    print("OK")
                    print("Player reached 5 seconds")
                }
            }
        }
    })

}


fileprivate var firstAppear = true
//////Swipe Gesture 
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.right:
            print("Swiped right")
        case UISwipeGestureRecognizerDirection.down:
            print("Swiped down")
        case UISwipeGestureRecognizerDirection.left:
            print("Swiped left")
        case UISwipeGestureRecognizerDirection.up:
            print("Swiped up")
        case UISwipeGestureRecognizerDirection.init(rawValue: 200):
            print("Swiped Custom")

        default:
            break
        }
    }
}    
/////////End Swipe Gesture
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let value = UIInterfaceOrientation.landscapeLeft.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")
    if firstAppear {
        do {
            try playBackgroundMovieVideo()
            firstAppear = false
        } catch AppError.invalidResource(let NMNF6327, let m4v) {
            debugPrint("Could not find resource \(NMNF6327).\(m4v)")
        } catch {
            debugPrint("Generic error")
        }


    }
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
    return UIInterfaceOrientationMask.landscapeLeft
}
fileprivate func playBackgroundMovieVideo() throws {
    guard let path = Bundle.main.path(forResource: "NMNF6327", ofType:"m4v") else {
        throw AppError.invalidResource("NMNF6327", "m4v")

    }

    self.Player = AVPlayer(url: URL(fileURLWithPath: path))
    let playerController = AVPlayerViewController()
    playerController.showsPlaybackControls = false
    playerController.view.isUserInteractionEnabled = true
    playerController.player = self.Player
    playerController.viewWillLayoutSubviews()
    playerController.allowsPictureInPicturePlayback = false



    myFirstButton.setImage(#imageLiteral(resourceName: "pause.png"), for: UIControlState.normal)
    myFirstButton.frame = CGRect(x: 5, y: 5, width: 70, height: 50)
    self.myFirstButton.addTarget(self, action:#selector(self.myFirstButtonpressed), for: .touchUpInside)
    self.view.addSubview(myFirstButton)

    playerController.view.addSubview(myFirstButton)

    mySecondButton.setImage(#imageLiteral(resourceName: "Options.png"), for: UIControlState.normal)
    mySecondButton.frame = CGRect(x: 60, y: 5, width: 70, height: 50)
    self.mySecondButton.addTarget(self, action:#selector(self.mySecondButtonClicked), for: .touchUpInside)
    self.view.addSubview(mySecondButton)

    playerController.view.addSubview(mySecondButton)


    self.present(playerController, animated: false) {
        self.Player.play()
    }
}

func playerDidReachEnd(notification: NSNotification) {
    self.Player.seek(to: kCMTimeZero)
    self.Player.play()
}

func myFirstButtonpressed(sender: UIButton!) {
    myFirstButton.setImage(#imageLiteral(resourceName: "Play.png"), for: UIControlState.normal)

    let alertView = UIAlertView();
    alertView.addButton(withTitle: "Continue");
    alertView.delegate=self;
    alertView.addButton(withTitle: "restart");
    alertView.addButton(withTitle: "Middle");
    alertView.title = "PAUSE";
    alertView.message = "";
    alertView.show();

    let playerController = AVPlayerViewController()
    playerController.viewWillLayoutSubviews()
    self.present(playerController , animated: true)
    self.Player.pause()

}

func mySecondButtonClicked(){

}





func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {


    if buttonIndex == 0
{

    self.Player.play()
    myFirstButton.setImage(#imageLiteral(resourceName: "pause.png"), for: UIControlState.normal)
    print("Continue")


}

    else if buttonIndex == 1 {
    self.Player.seek(to: kCMTimeZero)
     self.Player.play()
     myFirstButton.setImage(#imageLiteral(resourceName: "pause.png"), for: UIControlState.normal)


    }
    ////Middle
else if buttonIndex == 2 {
    myFirstButton.setImage(#imageLiteral(resourceName: "pause.png"), for: UIControlState.normal)
    let timeScale = self.Player.currentItem?.asset.duration.timescale;
    let time = CMTimeMakeWithSeconds( +9 , timeScale!)
    self.Player.seek(to: time, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)
    self.Player.play()
    }

}

override var shouldAutorotate: Bool{
    return false
}
func update() {
    myFirstButton.isHidden=false
}

}


enum AppError : Error {
case invalidResource(String, String)
    }

这些是我所有的代码,我已经在另一个项目中测试过它们,它们都能正常工作。 - Saeed Rahmatolahi
你已经将sView添加为子视图了吗? - Ollie
我也将sView添加为子视图,但在这里也没有得到答案。方法如下:self.view.addSubview(sView) - Saeed Rahmatolahi
使用AVPlayerViewController怎么样? - Jose Rosales
不,我只想使用滑动手势。 - Saeed Rahmatolahi
显示剩余2条评论
4个回答

4

您的代码中有两个错误。

1. 在错误的控制器视图上添加手势视图。 您将手势视图添加到了初始控制器上,而不是AVPlayerViewController()上,这会在呈现后被AVPlayerViewController()覆盖。

2. 错误的选择器目标 您对let swipeRight = UISwipeGestureRecognizer(target: sView, action: #selector(self.respondToSwipeGesture))target做出了错误的假设。您将目标设置为sView,并在ViewController上实现选择器方法。

这里的目标指选择器方法的目标对象。因此,将目标更改为self(即您的视图控制器)将使您的视图控制器成为选择器方法func respondToSwipeGesture(gesture: UIGestureRecognizer)的目标。

请参考以下已更正的代码。

import UIKit
import AVKit
import AVFoundation

class ViewController: UIViewController , UIAlertViewDelegate {

let myFirstButton = UIButton()
let mySecondButton = UIButton()
var scoreLabel = UILabel()
var Player = AVPlayer()
var swipeGesture = UIGestureRecognizer()
var sView = UIView()


override func viewDidLoad() {
    super.viewDidLoad()


    let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeRight.direction = UISwipeGestureRecognizerDirection.right
    self.sView.addGestureRecognizer(swipeRight)

    let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeLeft.direction = UISwipeGestureRecognizerDirection.left
    self.sView.addGestureRecognizer(swipeLeft)

    let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeUp.direction = UISwipeGestureRecognizerDirection.up
    self.sView.addGestureRecognizer(swipeUp)

    let swipeCustom = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeCustom.direction = UISwipeGestureRecognizerDirection.init(rawValue: 200)
    self.sView.addGestureRecognizer(swipeCustom)


    let swipeDown = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeDown.direction = UISwipeGestureRecognizerDirection.down
    self.sView.addGestureRecognizer(swipeDown)


    //////////////////////End Swipe Gesture

    let currentPlayerItem = Player.currentItem
    let duration = currentPlayerItem?.asset.duration
    let currentTime = Float(self.Player.currentTime().value)


    if currentTime >= 5 {

        print("OK")

    }else if currentTime <= 5 {

        print("NO")
    }

    NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.Player.currentItem, queue: nil, using: { (_) in
        DispatchQueue.main.async {
            self.Player.seek(to: kCMTimeZero)
            self.Player.play()
        }
    })



    /////////////
    NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.Player.currentItem, queue: nil, using: { (_) in
        DispatchQueue.main.async {
            self.Player.seek(to: kCMTimeZero)
            self.Player.play()
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5.0) {
                // check if player is still playing
                if self.Player.rate != 0 {
                    print("OK")
                    print("Player reached 5 seconds")
                }
            }
        }
    })

}


fileprivate var firstAppear = true
//////Swipe Gesture
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
    if let swipeGesture = gesture as? UISwipeGestureRecognizer {
        switch swipeGesture.direction {
        case UISwipeGestureRecognizerDirection.right:
            print("Swiped right")
        case UISwipeGestureRecognizerDirection.down:
            print("Swiped down")
        case UISwipeGestureRecognizerDirection.left:
            print("Swiped left")
        case UISwipeGestureRecognizerDirection.up:
            print("Swiped up")
        case UISwipeGestureRecognizerDirection.init(rawValue: 200):
            print("Swiped Custom")

        default:
            break
        }
    }
}
/////////End Swipe Gesture
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let value = UIInterfaceOrientation.landscapeLeft.rawValue
    UIDevice.current.setValue(value, forKey: "orientation")
    if firstAppear {
        do {
            try playBackgroundMovieVideo()
            firstAppear = false
        } catch AppError.invalidResource(let NMNF6327, let m4v) {
            debugPrint("Could not find resource \(NMNF6327).\(m4v)")
        } catch {
            debugPrint("Generic error")
        }


    }
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
    return UIInterfaceOrientationMask.landscapeLeft
}
fileprivate func playBackgroundMovieVideo() throws {
    guard let path = Bundle.main.path(forResource: "NMNF6327", ofType:"m4v") else {
        throw AppError.invalidResource("NMNF6327", "m4v")

    }

    self.Player = AVPlayer(url: URL(fileURLWithPath: path))
    let playerController = AVPlayerViewController()
    playerController.showsPlaybackControls = false
    playerController.view.isUserInteractionEnabled = true
    playerController.player = self.Player
    playerController.viewWillLayoutSubviews()
    playerController.allowsPictureInPicturePlayback = false



    myFirstButton.setImage(#imageLiteral(resourceName: "pause.png"), for: UIControlState.normal)
    myFirstButton.frame = CGRect(x: 5, y: 5, width: 70, height: 50)
    self.myFirstButton.addTarget(self, action:#selector(self.myFirstButtonpressed), for: .touchUpInside)
    self.view.addSubview(myFirstButton)


    mySecondButton.setImage(#imageLiteral(resourceName: "Options.png"), for: UIControlState.normal)
    mySecondButton.frame = CGRect(x: 60, y: 5, width: 70, height: 50)
    self.mySecondButton.addTarget(self, action:#selector(self.mySecondButtonClicked), for: .touchUpInside)
    self.view.addSubview(mySecondButton)


    sView.frame = self.view.frame
    playerController.view.addSubview(sView)
    playerController.view.bringSubview(toFront: sView)

    // ****** buttons are added after sview ********** 
    playerController.view.addSubview(myFirstButton)

    playerController.view.addSubview(mySecondButton)

    self.present(playerController, animated: false) {
        self.Player.play()
    }
}

func playerDidReachEnd(notification: NSNotification) {
    self.Player.seek(to: kCMTimeZero)
    self.Player.play()
}

func myFirstButtonpressed(sender: UIButton!) {
    myFirstButton.setImage(#imageLiteral(resourceName: "Play.png"), for: UIControlState.normal)

    let alertView = UIAlertView();
    alertView.addButton(withTitle: "Continue");
    alertView.delegate=self;
    alertView.addButton(withTitle: "restart");
    alertView.addButton(withTitle: "Middle");
    alertView.title = "PAUSE";
    alertView.message = "";
    alertView.show();

    let playerController = AVPlayerViewController()
    playerController.viewWillLayoutSubviews()
    self.present(playerController , animated: true)
    self.Player.pause()

}

func mySecondButtonClicked(){

}





func alertView(_ alertView: UIAlertView, clickedButtonAt buttonIndex: Int) {


    if buttonIndex == 0
    {

        self.Player.play()
        myFirstButton.setImage(#imageLiteral(resourceName: "pause.png"), for: UIControlState.normal)
        print("Continue")


    }

    else if buttonIndex == 1 {
        self.Player.seek(to: kCMTimeZero)
        self.Player.play()
        //myFirstButton.setImage(#imageLiteral(resourceName: "pause.png"), for: UIControlState.normal)


    }
        ////Middle
    else if buttonIndex == 2 {
       myFirstButton.setImage(#imageLiteral(resourceName: "pause.png"), for: UIControlState.normal)
        let timeScale = self.Player.currentItem?.asset.duration.timescale;
        let time = CMTimeMakeWithSeconds( +9 , timeScale!)
        self.Player.seek(to: time, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero)
        self.Player.play()
    }

}

override var shouldAutorotate: Bool{
    return false
}
func update() {
    myFirstButton.isHidden=false
}

 }


enum AppError : Error {
case invalidResource(String, String)
}

它已经工作了,但我无法使用按钮!我可以将它们添加到sview中吗? - Saeed Rahmatolahi
在添加按钮之前,先添加sview!请查看更新的代码。 - dip
请投票支持我的问题。 - Saeed Rahmatolahi

1

Swift 4

您可以不使用sView,只需向AVPlayerViewController的视图添加手势识别器即可:

func playVideo() {
    let playerController = AVPlayerViewController()
    playerController.player = AVPlayer(url: URL(fileURLWithPath: videoPath))
    playerController.showsPlaybackControls = false
    playerController.view.isUserInteractionEnabled = true

    let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(self.respondToSwipeGesture))
    swipeUp.direction = UISwipeGestureRecognizerDirection.up
    playerController.view.addGestureRecognizer(swipeUp)

    present(playerController, animated: false) {
        playerController.player?.play()
    }
}

@objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
    print("swipe up")
}

谢谢,这很有帮助!不幸的是,在较新的iOS(从11开始)上,滑动会导致控制器中的视频移动,并似乎优先于UISwipeGestureRecognizer。您知道有没有什么方法可以防止这种情况发生? - AnthoPak

0

可能需要正确初始化您的sView。确保将所需的框架分配给sView并将其作为子视图添加到self.view中。

override func viewDidLoad() {
    super.viewDidLoad()

    sView.frame = self.view.frame
    self.view.addSubview(sView)
    self.view.bringSubview(toFront: sView)

    // add you gestures to sView here ...
}

没有起作用,请帮我获得礼品声望。 - Saeed Rahmatolahi
我不明白问题出在哪里。我创建了一个带有AVPlayer和手势的示例项目,可以在这里找到。 - toma
我在GitHub上检查了你的代码,但对我仍然没有用,请帮助我获得50个声望。 - Saeed Rahmatolahi
好的,我在你的情况下唯一能做的就是调试你的代码示例,如果你与stackoverflow社区分享它。 - toma
好的,请等一下,今晚我会把所有的代码放在这里(编辑问题)。 - Saeed Rahmatolahi
我已经编辑了代码,现在你可以在这里看到我的所有代码,请帮助我获得50个声望。 - Saeed Rahmatolahi

0
你应该这样做:playerController.view.addSubview(self.sView) 而不是 self.view.addSubview(sView) 并且你应该像这样添加 self 作为目标: let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(respondToSwipeGesture)) 它会起作用的。这是我的控制台:https://istack.dev59.com/agaBx.webp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接