暂停/播放 AVPlayer 的问题
Issues pausing / playing an AVPlayer
我有一个 AVPlayer,我想在它被点击时暂停和恢复。
我决定解决这个问题的方法是在 AVPlayer 附加到的视图上使用 tapGestureRecognizer。
当点击布尔值时,变量 didSelect 变为真。当有第二次点击时,didSelect 变为 false。
有趣的是,当我使用
if didSelect == false {
player.play()
}
if didSelect == true {
player.pause()
}
在 AVPlayerItemDidPlayToEndTime 通知中,它工作正常,但仅在 视频完成后。这让我想知道是否有一些 AVPlayerItemIsPlaying 的通知,但我没有找到任何东西。
代码如下所示。
playerView.layer.cornerRadius = playerView.bounds.width * 0.025
guard let path = Bundle.main.path(forResource: "video", ofType:"mp4") else {
debugPrint("video.m4v not found")
return
}
let player = AVPlayer(url: URL(fileURLWithPath: path))
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = playerView.frame
playerLayer.frame = self.frame
playerLayer.frame = playerView.bounds
playerLayer.masksToBounds = true
playerLayer.cornerRadius = playerView.bounds.width * 0.025
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
self.playerView.layer.addSublayer(playerLayer)
player.isMuted = true
player.play()
if didSelect == false {
player.play()
}
if didSelect == true {
player.pause()
}
//looper
NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: nil)
{ notification in
let t1 = CMTimeMake(5, 100)
player.seek(to: t1)
player.play()
}
我遇到了同样的问题,最终创建了一个将播放控件嵌入到视频中的 AVPLayerViewController。
修改后的代码,希望对你有帮助。
import AVKit
import AVFoundation
let avController = AVPlayerViewController()
avController.showsPlaybackControls = true
let player = AVPlayer(url: URL(fileURLWithPath: path))
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = playerView.frame
playerLayer.frame = playerView.bounds
playerLayer.masksToBounds = true
playerLayer.cornerRadius = playerView.bounds.width * 0.025
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
avController.player = playerLayer
avController.view.frame = playerView.bounds
self.playerView.addSubview(avController.view)
我创建了对播放器的全局引用
var player: AVPlayer?
并在 UITapGestureRecognizer 函数中引用它,现在从那里控制暂停/播放。
我有一个 AVPlayer,我想在它被点击时暂停和恢复。
我决定解决这个问题的方法是在 AVPlayer 附加到的视图上使用 tapGestureRecognizer。
当点击布尔值时,变量 didSelect 变为真。当有第二次点击时,didSelect 变为 false。
有趣的是,当我使用
if didSelect == false {
player.play()
}
if didSelect == true {
player.pause()
}
在 AVPlayerItemDidPlayToEndTime 通知中,它工作正常,但仅在 视频完成后。这让我想知道是否有一些 AVPlayerItemIsPlaying 的通知,但我没有找到任何东西。
代码如下所示。
playerView.layer.cornerRadius = playerView.bounds.width * 0.025
guard let path = Bundle.main.path(forResource: "video", ofType:"mp4") else {
debugPrint("video.m4v not found")
return
}
let player = AVPlayer(url: URL(fileURLWithPath: path))
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = playerView.frame
playerLayer.frame = self.frame
playerLayer.frame = playerView.bounds
playerLayer.masksToBounds = true
playerLayer.cornerRadius = playerView.bounds.width * 0.025
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
self.playerView.layer.addSublayer(playerLayer)
player.isMuted = true
player.play()
if didSelect == false {
player.play()
}
if didSelect == true {
player.pause()
}
//looper
NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem, queue: nil)
{ notification in
let t1 = CMTimeMake(5, 100)
player.seek(to: t1)
player.play()
}
我遇到了同样的问题,最终创建了一个将播放控件嵌入到视频中的 AVPLayerViewController。
修改后的代码,希望对你有帮助。
import AVKit
import AVFoundation
let avController = AVPlayerViewController()
avController.showsPlaybackControls = true
let player = AVPlayer(url: URL(fileURLWithPath: path))
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = playerView.frame
playerLayer.frame = playerView.bounds
playerLayer.masksToBounds = true
playerLayer.cornerRadius = playerView.bounds.width * 0.025
playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
avController.player = playerLayer
avController.view.frame = playerView.bounds
self.playerView.addSubview(avController.view)
我创建了对播放器的全局引用
var player: AVPlayer?
并在 UITapGestureRecognizer 函数中引用它,现在从那里控制暂停/播放。