简体   繁体   English

Swift 3,XCode 8-按下按钮时更改本地视频-AVPlayer

[英]Swift 3, XCode 8 - Changing Local Video on Button Press - AVPlayer

I'm trying to create an app that will change videos on each button press. 我正在尝试创建一个将在每次按下按钮时更改视频的应用程序。

Currently: The video loops and the button does nothing 当前:视频循环播放,该按钮不起作用

Goals: The button will cycle to the next video based on the array index. 目标:该按钮将根据数组索引循环到下一个视频。 I plan on associating another array with text descriptions to go along with each video. 我计划将每个文本与另一个数组与文本描述相关联。

Thoughts: I think a JSON based implementation would probably work better. 思考:我认为基于JSON的实现可能会更好。 This is my first iOS app and I'm trying to keep it simple. 这是我的第一个iOS应用,我正在努力使其简单。 Thoughts and help are greatly appreciated! 的想法和帮助,不胜感激!

Here is the function that loops the video. 这是循环播放视频的功能。 The name of name of the video is held in an array called videoId: 视频名称的名称保存在名为videoId的数组中:

class WorkoutViewController: UIViewController{
@IBOutlet weak var videoView: UIView!
@IBOutlet weak var infoCardView: UIView!

var currentVidIndex: Int = 0    

var player: AVPlayer?
var playerLayer: AVPlayerLayer?    

override func viewDidLoad() {
    super.viewDidLoad()
    shadowBackground()
}
var videoId: [String] = ["bodyweight_fitness_arch","bodyweight_fitness_assisted_squat","bodyweight_fitness_band_dislocates"]


func setLoopingVideo(){
    let path = Bundle.main.path(forResource: videoId[currentVidIndex], ofType: "mp4")
    let url = URL.init(fileURLWithPath: path!)
    let player = AVPlayer(url: url)
    let playerLayer = AVPlayerLayer(player: player) 
    playerLayer.frame = videoView.bounds
    self.videoView.layer.insertSublayer(playerLayer, at: 0)
    player.play()

    // Create observer to monitor when video ends, when it does so set the video time to the start (zero)

    NotificationCenter.default.addObserver(forName: NSNotification.Name.AVPlayerItemDidPlayToEndTime,object: player.currentItem, queue: nil)

    {
        Notification in player.seek(to: kCMTimeZero)
        player.play()  
    }   

func shadowBackground(){
    infoCardView.layer.cornerRadius = 3.0
    infoCardView.layer.shadowColor = 
           UIColor.black.withAlphaComponent(0.6).cgColor
    infoCardView.layer.shadowOffset = CGSize(width: 0, height: 0)
    infoCardView.layer.shadowOpacity = 0.9        

    videoView.layer.cornerRadius = 3.0
    videoView.layer.shadowColor = 
    UIColor.black.withAlphaComponent(0.6).cgColor
    videoView.layer.shadowOffset = CGSize(width: 0, height: 0)
    videoView.layer.shadowOpacity = 0.9

}
override func viewDidAppear(_ animated: Bool) {
    setLoopingVideo()
}

@IBAction func showNextVideo(_ sender: UIButton) {
    if currentVidIndex < videoId.count{
        currentVidIndex += 1
        print (currentVidIndex)
    } else {
        NSLog("Nothing")
    }
}

What it looks like 看起来像什么

If you want to change a video on every button click, you have to create always new AVPlayer object. 如果要在每次单击按钮时更改视频,则必须始终创建新的AVPlayer对象。

  1. create and remember your AVPlayer object 创建并记住您的AVPlayer对象
  2. create and remember your AVPlayerLayer 创建并记住您的AVPlayerLayer

-- when button clicked -- -单击按钮时-

  1. remove AVPlayerLayer from the parent 从父级移除AVPlayerLayer
  2. stop AVPlayer (old video) 停止AVPlayer(旧视频)
  3. goto step 1 with new video URL 转到带有新视频网址的第1步

Here was my solution: 这是我的解决方案:

func clearVid(){
    if let player = self.player{
        player.pause()
        self.player = nil

    }
    if let layer = self.playerLayer{
        layer.removeFromSuperlayer()
        self.playerLayer = nil
    }

   self.videoView.layer.sublayers?.removeAll()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM