Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import AVFoundation
- class VideoPlayerViewController: UIViewController {
- // Video player view
- let playerView = UIView()
- // AVPlayer instance
- var player: AVPlayer?
- // Video URL
- var videoURL: URL?
- // Buttons and controls
- let playPauseButton = UIButton(type: .system)
- let stopButton = UIButton(type: .system)
- let rewindButton = UIButton(type: .system)
- let fastForwardButton = UIButton(type: .system)
- let volumeSlider = UISlider()
- let timeLabel = UILabel()
- let currentTimeLabel = UILabel()
- // Video playback state
- var isPlaying = false
- override func viewDidLoad() {
- super.viewDidLoad()
- // Set up player view
- playerView.translatesAutoresizingMaskIntoConstraints = false
- view.addSubview(playerView)
- NSLayoutConstraint.activate([
- playerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
- playerView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
- playerView.topAnchor.constraint(equalTo: view.topAnchor),
- playerView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
- ])
- // Set up buttons and controls
- playPauseButton.setTitle("Play", for: .normal)
- playPauseButton.addTarget(self, action: #selector(playPauseButtonTapped), for: .touchUpInside)
- stopButton.setTitle("Stop", for: .normal)
- stopButton.addTarget(self, action: #selector(stopButtonTapped), for: .touchUpInside)
- rewindButton.setTitle("Rewind", for: .normal)
- rewindButton.addTarget(self, action: #selector(rewindButtonTapped), for: .touchUpInside)
- fastForwardButton.setTitle("Fast Forward", for: .normal)
- fastForwardButton.addTarget(self, action: #selector(fastForwardButtonTapped), for:
- .touchUpInside)
- volumeSlider.minimumValue = 0
- volumeSlider.maximumValue = 1
- volumeSlider.value = 0.5
- volumeSlider.addTarget(self, action: #selector(volumeSliderValueChanged), for: .valueChanged)
- timeLabel.text = "00:00"
- currentTimeLabel.text = "00:00"
- // Add buttons and controls to the view
- let buttonStackView = UIStackView(arrangedSubviews: [playPauseButton, stopButton, rewindButton,
- fastForwardButton])
- buttonStackView.axis = .horizontal
- buttonStackView.distribution = .fillEqually
- buttonStackView.spacing = 10
- let controlStackView = UIStackView(arrangedSubviews: [volumeSlider, timeLabel,
- currentTimeLabel])
- controlStackView.axis = .vertical
- controlStackView.distribution = .fillProportionally
- controlStackView.spacing = 10
- view.addSubview(buttonStackView)
- view.addSubview(controlStackView)
- NSLayoutConstraint.activate([
- buttonStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
- buttonStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
- buttonStackView.topAnchor.constraint(equalTo: playerView.bottomAnchor, constant: 20),
- controlStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
- controlStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
- controlStackView.topAnchor.constraint(equalTo: buttonStackView.bottomAnchor, constant: 10)
- ])
- }
- // Play/pause button tapped
- @objc func playPauseButtonTapped() {
- if isPlaying {
- player?.pause()
- playPauseButton.setTitle("Play", for: .normal)
- isPlaying = false
- } else {
- player?.play()
- playPauseButton.setTitle("Pause", for: .normal)
- isPlaying = true
- }
- }
- // Stop button tapped
- @objc func stopButtonTapped() {
- player?.stop()
- player?.seek(to: CMTime.zero)
- currentTimeLabel.text = "00:00"
- isPlaying = false
- }
- // Rewind button tapped
- @objc func rewindButtonTapped() {
- guard let player = player else { return }
- let currentTime = player.currentTime()
- let newTime = currentTime - 10
- if newTime < 0 {
- player.seek(to: CMTime.zero)
- } else {
- player.seek(to: newTime)
- }
- }
- // Fast forward button tapped
- @objc func fastForwardButtonTapped() {
- guard let player = player else { return }
- let currentTime = player.currentTime()
- let duration = player.currentItem?.duration
- let newTime = min(currentTime + 10, duration)
- player.seek(to: newTime)
- }
- // Volume slider value changed
- @objc func volumeSliderValueChanged() {
- guard let player = player else { return }
- player.volume = volumeSlider.value
- }
- // Set up video playback
- func setupVideoPlayback(url: URL) {
- videoURL = url
- player = AVPlayer(url: videoURL)
- // Create a player layer and add it to the player view
- let playerLayer = AVPlayerLayer(player: player)
- playerLayer.frame = playerView.bounds
- playerView.layer.addSublayer(playerLayer)
- // Add observers for playback state changes
- NotificationCenter.default.addObserver(self, selector: #selector(playbackStateChanged), name:
- .AVPlayerItemDidPlayToEndTime, object: player?.currentItem)
- }
- // Playback state changed (e.g. video ended)
- @objc func playbackStateChanged() {
- stopButtonTapped()
- }
- }
- This `UIViewController` has the following features:
- * A `UIView` to display the video
- * An `AVPlayer` instance to play the video
- * Buttons for playing/pausing, stopping, rewinding, and fast forwarding
- * A volume slider to control the audio level
- * Labels to display the current time and duration of the video
- To use this view controller, simply create an instance of it and call the `setupVideoPlayback` method
- with a valid `URL` for the video. For example:
- ```swift
- let videoViewController = VideoPlayerViewController()
- videoViewController.setupVideoPlayback(url: URL(string: "https://example.com/video.mp4")!)
- ```
- Note that this is just an example implementation, and you may want to customize it to fit your specific
- needs (e.g. add error handling, display video thumbnails, etc.).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement