AVPlayer顶部的绘制按钮 [英] Draw button on top of AVPlayer

查看:95
本文介绍了AVPlayer顶部的绘制按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在视频relay next previous , leave comment的顶部绘制标签或按钮.视频列表中有该列表,一旦用户从表中选择一项,就需要播放;播放器播放完毕后,这些按钮或标签应位于视频顶部

I have to draw a label or button on top of video relay next previous , leave comment . List of video have it, once user select one item from the table,it need to play, Once player play finished, those buttons or label should come on top of video

这是我的代码:

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

这样的要求

推荐答案

您正在使用AVPlayerViewController,因此没有理由像Alessandro Ornano的答案那样访问应用程序的窗口.为什么要重新发明轮子?每个AVPlayerViewController都有一个

You're using an AVPlayerViewController, so there's no reason to access your application's window like in Alessandro Ornano's answer. Why reinvent the wheel? Every AVPlayerViewController has a contentOverlayView property which allows you to place views between the player and the controls.

首先,创建一个新的AVPlayerItem并监听该项目上的AVPlayerItemDidPlayToEndTimeNotification通知.将该项目加载到播放器中,然后开始播放.

First, create a new AVPlayerItem and listen for the AVPlayerItemDidPlayToEndTimeNotification notification on that item. Load the item into your player and begin playback.

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

Once the item completes, the selector your specified to listen for the AVPlayerItemDidPlayToEndTimeNotification notification will be called. In that selector, access the contentOverlayView directly and add your buttons:

在某些视图控制器或其他对象中:

In some view controller or other object:

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
}

如评论中所述(以及拒绝的编辑),按钮可能无法在contentOverlayView中使用.有关替代解决方案,请参见 Pyro的答案.

As stated in the comments (and a rejected edit), buttons may not work in the contentOverlayView. For an alternate solution, see Pyro's answer.

您还可以将AVPlayerViewController子类化,并在子类的实例内执行所有操作,但是Apple 对此进行警告:

You could also subclass AVPlayerViewController and do everything inside an instance of your subclass, but Apple warns against that:

请勿将AVPlayerViewController子类化.不支持覆盖此类的方法,并且会导致未定义的行为.

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

这篇关于AVPlayer顶部的绘制按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆