gpt4 book ai didi

ios - 在 AVPlayer 顶部绘制按钮

转载 作者:IT王子 更新时间:2023-10-29 05:47:15 25 4
gpt4 key购买 nike

我必须在视频顶部绘制标签或按钮 relay next previous , leave comment 。视频列表有它,一旦用户从表中选择一个项目,它需要播放,播放器播放完成后,那些按钮或标签应该出现在视频顶部

这是我的代码:

comPlayerControl = AVPlayerViewController()

if let player = comPlayerControl {

let videoURL: String = "http://cdnapi.kaltura.com/p/11/sp/11/playManifest/entryId/"+selectedSubmission.transcodeRefId+"/format/applehttp/protocol/http/a.m3u8"
let playerItem = AVPlayerItem(URL: NSURL(string: videoURL)! )
commmentPlayer = AVPlayer(playerItem: playerItem)
player.player = commmentPlayer
player.view.frame = videoCell.frame
player.view.sizeToFit()

player.showsPlaybackControls = true
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: #selector(CommentsTableViewController.playerDidFinishPlaying(_:)),
name: AVPlayerItemDidPlayToEndTimeNotification,
object: playerItem
)

comPlayerControl.delegate = self
videoCell.addSubview(player.view)
}

func playerDidFinishPlaying(note: NSNotification) {
print("Video Finished")
let DynamicView=UIView(frame: CGRectMake(100, 200, 100, 100))
DynamicView.backgroundColor=UIColor.greenColor()
DynamicView.layer.cornerRadius=25
DynamicView.layer.borderWidth=2
DynamicView.layer.zPosition = 1;
comPlayerControl.view.addSubview(DynamicView)
}

这样的要求

requirement image

最佳答案

您正在使用 AVPlayerViewController,因此没有理由像 Alessandro Ornano 的回答那样访问应用程序的窗口。为什么要重新发明轮子?每个 AVPlayerViewController 都有一个 contentOverlayView允许您在播放器和控件之间放置 View 的属性。

首先,创建一个新的 AVPlayerItem 并监听关于该项目的 AVPlayerItemDidPlayToEndTimeNotification 通知。将项目加载到播放器并开始播放。

项目完成后,您指定用于监听 AVPlayerItemDidPlayToEndTimeNotification 通知的选择器将被调用。在该选择器中,直接访问 contentOverlayView 并添加您的按钮:

在一些 View Controller 或其他对象中:

let playerVC = AVPlayerViewController()

// ...

func setupPlayer {

let playerItem = AVPlayerItem(...)
playerVC.player?.replaceCurrentItemWithPlayerItem(playerItem)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(VC.itemFinished), name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem)
self.presentViewController(playerVC, animated: true) {
self.playerVC.player?.play()
}
}

func itemFinished() {
let btn = UIButton(type: .System)
btn.addTarget(self, action: #selector(VC.buttonTapped), forControlEvents: .TouchUpInside)
self.playerVC.contentOverlayView?.addSubview(btn)
}

func buttonTapped() {
print("button was tapped")
// replay/comment logic here
}

如评论(和 rejected edit)中所述,按钮在 contentOverlayView 中可能不起作用。有关替代解决方案,请参阅 Pyro's answer .

您还可以子类化 AVPlayerViewController 并在子类的实例中执行所有操作,但 Apple warns against that :

Do not subclass AVPlayerViewController. Overriding this class’s methods is unsupported and results in undefined behavior.

关于ios - 在 AVPlayer 顶部绘制按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36368673/

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