我正在嘗試創建一個應用程序,它將更改每個按鈕按下的視頻。Swift 3,XCode 8 - 更改按鈕按下本地視頻 - AVPlayer
目前:視頻循環和按鈕不執行任何操作
目標:按鈕,將循環基於數組的索引下一個視頻。我計劃將另一個陣列與文字說明關聯起來,以便與每個視頻一起播放。
想法:我認爲基於JSON的實現可能會更好。這是我的第一個iOS應用程序,我試圖保持簡單。非常感謝您的意見和幫助!
以下是循環播放視頻的功能。視頻的名稱的名稱已在陣列中叫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")
}
}
謝謝!我懷疑我不得不把它當作一個物體來對待,我只是希望我不必:P – Lancaster
如果有人感興趣: 'func removeVid(){ if let player = self.player { player。暫停() self.player =零 } 如果讓層= self.playerLayer { layer.removeFromSuperlayer() self.playerLayer =零 } self.videoView.layer.sublayers?.removeAll() }' – Lancaster