Draw button on top of AVPlayer

前端 未结 5 2086
执念已碎
执念已碎 2020-12-28 09:29

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 t

5条回答
  •  我在风中等你
    2020-12-28 10:23

    Based on the your question and from the comment/code of Ramis i have made a sample code which you may try

    As mentioned by JAL the contentOverlayView should be the best option to display the control over the video in the AVPlayerController, but as per my sample demo the contentOverlayView don't have any user interaction for the buttons or other controls, as if you check in the 3D view of the AVPlayerController it has AVTouchIgnoringView/UIView in front of the contentOverlayView which may be problem in user interaction with contentOverlayView.

    So another solution is to add the overlay view in the AVPlayerViewController

    func addContentOverlayView() {
    
        OverlayView.frame = CGRectMake(0,30,AVPlayerVC.view.bounds.width, 100)
        OverlayView.hidden = true
        OverlayView.backgroundColor = UIColor ( red: 0.5, green: 0.5, blue: 0.5, alpha: 0.379 )
    
        let btnNext = UIButton(frame:CGRectMake(AVPlayerVC.view.bounds.width - 60,0,60,44))
        btnNext.setTitle(">>", forState:.Normal)
        btnNext.addTarget(self, action:"playNext", forControlEvents:.TouchUpInside)
        //        btnNext.layer.borderColor = UIColor ( red: 0.0, green: 0.0, blue: 1.0, alpha: 0.670476140202703 ).CGColor
        //        btnNext.layer.borderWidth = 1.0
        OverlayView.addSubview(btnNext)
    
        let btnReplay = UIButton(frame:CGRectMake((AVPlayerVC.view.bounds.width/2)-40,0,80,44))
        btnReplay.setTitle("Replay", forState:.Normal)
        btnReplay.addTarget(self, action:"replayVideo", forControlEvents:.TouchUpInside)
        OverlayView.addSubview(btnReplay)
    
        let btnPrevious = UIButton(frame:CGRectMake(0,0,80,44))
        btnPrevious.setTitle("<<", forState:.Normal)
        btnPrevious.addTarget(self, action:"previousVideo", forControlEvents:.TouchUpInside)
        OverlayView.addSubview(btnPrevious)
    
        let btnComment = UIButton(frame:CGRectMake((AVPlayerVC.view.bounds.width/2)-70,40,140,44))
        btnComment.setTitle("Comments", forState:.Normal)
        btnComment.addTarget(self, action:"openComments", forControlEvents:.TouchUpInside)
        OverlayView.addSubview(btnComment)
    
        AVPlayerVC.view.addSubview(OverlayView);
    
    }
    
    func playNext() {
        prevItem = AVPlayerVC.player?.currentItem
        OverlayView.hidden = true
        commmentQueuePlayer.advanceToNextItem()
    }
    
    func replayVideo() {
        OverlayView.hidden = true
        AVPlayerVC.player?.currentItem?.seekToTime(kCMTimeZero)
        AVPlayerVC.player?.play()
    }
    
    func previousVideo() {
        OverlayView.hidden = true
        if prevItem != AVPlayerVC.player?.currentItem {
            if (commmentQueuePlayer.canInsertItem(prevItem!, afterItem:AVPlayerVC.player?.currentItem)) {
                //commmentQueuePlayer.insertItem(prevItem!, afterItem:AVPlayerVC.player?.currentItem)
                commmentQueuePlayer.replaceCurrentItemWithPlayerItem(prevItem)
                prevItem = AVPlayerVC.player?.currentItem
                replayVideo()
            }
        } else {
            replayVideo()
            //Else display alert no prev video found
        }
    }
    
    func stopedPlaying() {
        if prevItem == nil {
            prevItem = AVPlayerVC.player?.currentItem
        }
        OverlayView.hidden = false
    }
    

    At the initial setup we set the AVPlayerController,AVQueuePlayer etc... at that time we can add the overlay on the AVPlayerController

    For the previous item there is no direct available and as per documentation the item will be remove once it's next item is played , so we have two option like replaceCurrentItemWithPlayerItem or insertItem(item: AVPlayerItem, afterItem: AVPlayerItem?)

    If you need to check complete code you can check it from : https://gist.github.com/Pyrolr/debb4fca8f608b1300e099a5b3547031

    Note: This is just like prototype, it is not working perfectly in all the cases but it can help you in understanding the basic functionality you wnat and you can improve/optimise based on your requirements

提交回复
热议问题