Advertisement
marcusziade

llama 3 70B sample

May 13th, 2024
1,579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 6.14 KB | None | 0 0
  1.  
  2. import UIKit
  3. import AVFoundation
  4.  
  5. class VideoPlayerViewController: UIViewController {
  6.     // Video player view
  7.     let playerView = UIView()
  8.  
  9.     // AVPlayer instance
  10.     var player: AVPlayer?
  11.  
  12.     // Video URL
  13.     var videoURL: URL?
  14.  
  15.     // Buttons and controls
  16.     let playPauseButton = UIButton(type: .system)
  17.     let stopButton = UIButton(type: .system)
  18.     let rewindButton = UIButton(type: .system)
  19.     let fastForwardButton = UIButton(type: .system)
  20.     let volumeSlider = UISlider()
  21.     let timeLabel = UILabel()
  22.     let currentTimeLabel = UILabel()
  23.  
  24.     // Video playback state
  25.     var isPlaying = false
  26.  
  27.     override func viewDidLoad() {
  28.         super.viewDidLoad()
  29.  
  30.         // Set up player view
  31.         playerView.translatesAutoresizingMaskIntoConstraints = false
  32.         view.addSubview(playerView)
  33.         NSLayoutConstraint.activate([
  34.             playerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
  35.             playerView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
  36.             playerView.topAnchor.constraint(equalTo: view.topAnchor),
  37.             playerView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
  38.         ])
  39.  
  40.         // Set up buttons and controls
  41.         playPauseButton.setTitle("Play", for: .normal)
  42.         playPauseButton.addTarget(self, action: #selector(playPauseButtonTapped), for: .touchUpInside)
  43.         stopButton.setTitle("Stop", for: .normal)
  44.         stopButton.addTarget(self, action: #selector(stopButtonTapped), for: .touchUpInside)
  45.         rewindButton.setTitle("Rewind", for: .normal)
  46.         rewindButton.addTarget(self, action: #selector(rewindButtonTapped), for: .touchUpInside)
  47.         fastForwardButton.setTitle("Fast Forward", for: .normal)
  48.         fastForwardButton.addTarget(self, action: #selector(fastForwardButtonTapped), for:
  49. .touchUpInside)
  50.  
  51.         volumeSlider.minimumValue = 0
  52.         volumeSlider.maximumValue = 1
  53.         volumeSlider.value = 0.5
  54.         volumeSlider.addTarget(self, action: #selector(volumeSliderValueChanged), for: .valueChanged)
  55.  
  56.         timeLabel.text = "00:00"
  57.         currentTimeLabel.text = "00:00"
  58.  
  59.         // Add buttons and controls to the view
  60.         let buttonStackView = UIStackView(arrangedSubviews: [playPauseButton, stopButton, rewindButton,
  61. fastForwardButton])
  62.         buttonStackView.axis = .horizontal
  63.         buttonStackView.distribution = .fillEqually
  64.         buttonStackView.spacing = 10
  65.  
  66.         let controlStackView = UIStackView(arrangedSubviews: [volumeSlider, timeLabel,
  67. currentTimeLabel])
  68.         controlStackView.axis = .vertical
  69.         controlStackView.distribution = .fillProportionally
  70.         controlStackView.spacing = 10
  71.  
  72.         view.addSubview(buttonStackView)
  73.         view.addSubview(controlStackView)
  74.  
  75.         NSLayoutConstraint.activate([
  76.             buttonStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
  77.             buttonStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
  78.             buttonStackView.topAnchor.constraint(equalTo: playerView.bottomAnchor, constant: 20),
  79.  
  80.             controlStackView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 20),
  81.             controlStackView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -20),
  82.             controlStackView.topAnchor.constraint(equalTo: buttonStackView.bottomAnchor, constant: 10)
  83.         ])
  84.     }
  85.  
  86.     // Play/pause button tapped
  87.     @objc func playPauseButtonTapped() {
  88.         if isPlaying {
  89.             player?.pause()
  90.             playPauseButton.setTitle("Play", for: .normal)
  91.             isPlaying = false
  92.         } else {
  93.             player?.play()
  94.             playPauseButton.setTitle("Pause", for: .normal)
  95.             isPlaying = true
  96.         }
  97.     }
  98.  
  99.     // Stop button tapped
  100.     @objc func stopButtonTapped() {
  101.         player?.stop()
  102.         player?.seek(to: CMTime.zero)
  103.         currentTimeLabel.text = "00:00"
  104.         isPlaying = false
  105.     }
  106.  
  107.     // Rewind button tapped
  108.     @objc func rewindButtonTapped() {
  109.         guard let player = player else { return }
  110.         let currentTime = player.currentTime()
  111.         let newTime = currentTime - 10
  112.         if newTime < 0 {
  113.             player.seek(to: CMTime.zero)
  114.         } else {
  115.             player.seek(to: newTime)
  116.         }
  117.     }
  118.  
  119.     // Fast forward button tapped
  120.     @objc func fastForwardButtonTapped() {
  121.         guard let player = player else { return }
  122.         let currentTime = player.currentTime()
  123.         let duration = player.currentItem?.duration
  124.         let newTime = min(currentTime + 10, duration)
  125.         player.seek(to: newTime)
  126.     }
  127.  
  128.     // Volume slider value changed
  129.     @objc func volumeSliderValueChanged() {
  130.         guard let player = player else { return }
  131.         player.volume = volumeSlider.value
  132.     }
  133.  
  134.     // Set up video playback
  135.     func setupVideoPlayback(url: URL) {
  136.         videoURL = url
  137.         player = AVPlayer(url: videoURL)
  138.  
  139.         // Create a player layer and add it to the player view
  140.         let playerLayer = AVPlayerLayer(player: player)
  141.         playerLayer.frame = playerView.bounds
  142.         playerView.layer.addSublayer(playerLayer)
  143.  
  144.         // Add observers for playback state changes
  145.         NotificationCenter.default.addObserver(self, selector: #selector(playbackStateChanged), name:
  146. .AVPlayerItemDidPlayToEndTime, object: player?.currentItem)
  147.     }
  148.  
  149.     // Playback state changed (e.g. video ended)
  150.     @objc func playbackStateChanged() {
  151.         stopButtonTapped()
  152.     }
  153. }
  154.  
  155. This `UIViewController` has the following features:
  156.  
  157. * A `UIView` to display the video
  158. * An `AVPlayer` instance to play the video
  159. * Buttons for playing/pausing, stopping, rewinding, and fast forwarding
  160. * A volume slider to control the audio level
  161. * Labels to display the current time and duration of the video
  162.  
  163. To use this view controller, simply create an instance of it and call the `setupVideoPlayback` method
  164. with a valid `URL` for the video. For example:
  165. ```swift
  166. let videoViewController = VideoPlayerViewController()
  167. videoViewController.setupVideoPlayback(url: URL(string: "https://example.com/video.mp4")!)
  168. ```
  169. Note that this is just an example implementation, and you may want to customize it to fit your specific
  170. needs (e.g. add error handling, display video thumbnails, etc.).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement