【问题标题】:how to have separate videos that play with separate buttons如何使用单独的按钮播放单独的视频
【发布时间】:2020-05-10 01:49:47
【问题描述】:

谁能帮帮我,我正在尝试添加单独的按钮以在同一个视图控制器中播放单独的视频,但我不知道怎么做。

这是我的代码,我该怎么做?

导入 UIKit 导入 AVKit

类视图控制器:UIViewController {

@IBAction func Town(_ sender: Any) {

if let path = Bundle.main.path(forResource: "grey", ofType: "mov") {


        let video = AVPlayer(url: URL(fileURLWithPath: path))
        let videoPlayer = AVPlayerViewController()

        videoPlayer.player = video

        self.present(videoPlayer, animated: true, completion: {
            video.play()
        })

}
    func viewDidLoad() {
    super.viewDidLoad()

}

}

}

【问题讨论】:

  • “正在尝试添加单独的按钮...但我不知道怎么做。” 使用 Google 搜索在几秒钟内找到 this useful thread。学习如何创建按钮后,使用targetAction 运行一个函数,该函数设置要播放的视频文件名并执行video.play() 部分。

标签: swift video uiviewcontroller uibutton xcode11


【解决方案1】:

要在同一个 viewController 中同时播放 2 个视频,您需要在 Storyboard 中创建 2 个单独的视图和 2 个相应的按钮。 其余功能将进入您的 IBActions。 请使用以下代码:

class VideoPlaybackViewController: UIViewController {

@IBOutlet weak var videoView1: UIView!
@IBOutlet weak var videoView2: UIView!

@IBAction func playFirstVideo(_ sender: Any) {
    guard let path = Bundle.main.path(forResource: "640", ofType: "mov") else {
        print("Video Source Not Found")
        return
    }
    playVideo(playbackURL: URL(fileURLWithPath: path), playerView: videoView1)
}

@IBAction func playSecondVideo(_ sender: Any) {
    guard let path = Bundle.main.path(forResource: "720", ofType: "mov") else {
        print("Video Source Not Found")
        return
    }
    playVideo(playbackURL: URL(fileURLWithPath: path), playerView: videoView2)
}

func playVideo(playbackURL: URL, playerView: UIView) {
    let player = AVPlayer(url: playbackURL)
    let playerLayer = AVPlayerLayer(player: player)
    playerLayer.frame = playerView.bounds
    playerView.layer.addSublayer(playerLayer)
    player.play()
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
}
}

Please see the screen with this implementation

【讨论】:

    【解决方案2】:

    我用过这段代码,效果很好

    导入 UIKit 导入 AVKit

    类视图控制器:UIViewController {

    @IBOutlet weak var videoView1: UIButton!
    @IBOutlet weak var videoView2: UIButton!
    
    @IBAction func playFirstVideo(_ sender: Any) {
          if let path = Bundle.main.path(forResource: "grey", ofType: "mov") {
    
               let video = AVPlayer(url: URL(fileURLWithPath: path))
               let videoPlayer = AVPlayerViewController()
    
               videoPlayer.player = video
    
               self.present(videoPlayer, animated: true, completion: {
                   video.play()
               })
    
        }
    }
    
    
    @IBAction func playSecondVideo(_ sender: Any) {
      if let path = Bundle.main.path(forResource: "go", ofType: "mov") {
    
           let video = AVPlayer(url: URL(fileURLWithPath: path))
           let videoPlayer = AVPlayerViewController()
    
           videoPlayer.player = video
    
           self.present(videoPlayer, animated: true, completion: {
               video.play()
           })
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2014-03-05
      • 2022-01-23
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多