This is the 9th day of my participation in the August More Text Challenge. For details, see:August is more challenging
preface
Last time we had the video play done, now let’s do the control layer. So what controls do we need?
1. Swipe up and down on the left side of the screen to set the brightness; 2. Swipe up and down on the right side of the screen to set the volume; 3
Control function
1, at firstPlayerViewController
Add one insidePanDirection
Enumeration to identify the slide direction
Enum PanDirection {case horizontal // horizontal // vertical // horizontal // horizontal}Copy the code
2, toplayer.view
Add click and double click gestures
let singleTap = UITapGestureRecognizer(target: self, action: #selector(handleSingleTap)) singleTap.delegate = self singleTap.numberOfTapsRequired = 1 player.view.addGestureRecognizer(singleTap) let doubleTap = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap)) doubleTap.delegate = self doubleTap.numberOfTapsRequired = 2 Player. View. AddGestureRecognizer (doubleTap) / / / solve the click when the current view in response to other controls events singleTap. DelaysTouchesBegan = true DoubleTap. DelaysTouchesBegan = true / / / double failure response to the click event singleTap. The require (toFail: DoubleTap) /// Click @objc private Func handleSingleTap() {} /// Double-click @objc private Func handleDoubleTap() {switch player.playbackState { case .stopped: player.playFromBeginning() case .paused: player.playFromCurrentTime() case .playing: player.pause() case .failed: player.pause() } }Copy the code
3. Add when the video is played successfullyUIPanGestureRecognizer
gestures
func playerReady(_ player: Player) { let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panDirection(_:))) panRecognizer.delegate = self panRecognizer.maximumNumberOfTouches = 1 panRecognizer.delaysTouchesBegan = true panRecognizer.delaysTouchesEnded = true panRecognizer.cancelsTouchesInView = true player.view.addGestureRecognizer(panRecognizer) } @objc private func panDirection(_ pan: UIPanGestureRecognizer) {/// let locationPoint = pan.location(in: Let veloctyPoint = pan.velocity(in: Switch pan.state {case. Began:; player. View) // switch Pan.state {case. Let x = abs(veloctyPoint. X) let y = abs(veloctyPoint. Y) if x > y {/// horizontal panDirection =.horizontal sumTime = Double(player.currentTimeInterval) isPlaying = player.playbackState == .playing player.pause() isDragged = True} else {panDirection =. Vertical / / / began to slide to is to control the volume if (locationPoint. X > player. The bounds, the size, width / 2) { self.isVolume = true; } else {self.isVolume = false; self.isVolume = false; } } case .changed: switch panDirection { case .horizontal: horizontalMoved(value: veloctyPoint.x) case .vertical: VerticalMoved (value: veloctyPoint.y) default: break} case. Ended: switch panDirection { case .horizontal: if isPlaying { player.playFromCurrentTime() isPlaying = false } isDragged = false sumTime = 0 case .vertical: IsVolume = false default: break} default: Private func verticalMoved(value: CGFloat) {isVolume? (volumeViewSlider? .value -= Float(value / 10000)) : (uiscreen.main.brightness -= value / 10000)} private func horizontalMoved(value: CGFloat) {sumTime += Double(value) / 300 let totalTime = player.maximumDuration if sumTime > totalTime { sumTime = totalTime } if sumTime < 0 { sumTime = 0 } player.seek(to: CMTime(seconds: sumTime, preferredTimescale: CMTimeScale(1 * NSEC_PER_SEC))) } extension PlayerViewController: UIGestureRecognizerDelegate { func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool { guard let v = touch.view else { return false } if v.isKind(of: UISlider.self) { return false } if v.isKind(of: UIButton.self) { return false } return true } }Copy the code
Get the UISlider in the MPVolumeView to control the volume before setting the volume
let volumeView = MPVolumeView() volumeViewSlider = nil for v in volumeView.subviews { if v.classForCoder.description() == "MPVolumeSlider", let slider = v as? UISlider {volumeViewSlider = slider}} do {/// Can be shown under the silent voice try AVAudioSession. SharedInstance () setCategory (AVAudioSession. Category. Playback)} catch {}Copy the code
Here’s how it works. Setting the screen brightness during screen recording has no effect on the video after recording