gpt4 book ai didi

ios - 如何在 swift 3 中将滑动手势添加到 AVPlayer

转载 作者:可可西里 更新时间:2023-11-01 05:32:50 25 4
gpt4 key购买 nike

我正在使用 Swift 3,我想向 AVPlayer 添加滑动手势。有人告诉我,为了做到这一点,我必须使用另一个 View 并将该 View 带到视频的顶部 - 所以我做到了,这是我的代码:(但没有用 ) :(

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)
}

最佳答案

你的代码有两个错误。

<强>1。在错误的 Controller View 上添加手势 View 。 您没有将手势 View 添加到 AVPlayerViewController(),而是在初始 Controller 上添加,它将在呈现后被 AVPlayerViewController() 覆盖。

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

这里的目标是选择器方法的目标对象。因此,将目标更改为 self (即您的 View Controller )将使您的 View Controller 成为选择器方法的目标 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)
}

关于ios - 如何在 swift 3 中将滑动手势添加到 AVPlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44288116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com