#まずはArrayの定義。から、Videosのプレイイベントをボタン5種類で発火
ボタンで、NSIntegerで定義した、整数iiに数値を代入し、playVideo()イヴェントへ橋渡し
Playerのセットと、Videoを再生するViewの作成、とコントローラーの作成。
その後、のEndedイベントの作成、と、ii のcount up 、
バックグラウンドモードの作成。(General パネルの中の、BackgroundModeをCapabilityから追加しておく必要がある)
import UIKit
import AVFoundation
import AVKit
class ViewController: UIViewController(){
@IBOutlet weak scroll1:UIScrollView!
var arr = NSArray()
var arr2 = NSArray()
var player = AVPlayer()
var controller = AVPlayerViewController()
var ii = NSInteger()
override viewDidLoad() {
arr = ["url1.mp4", "url2.mp4", "url3.mp4", "url4.mp4", "url5.mp4"]
arr2 = ["Video Name1", "Video Name2", "Video Name3", "Video Name4", "Video Name5"]
scroll1.isScrollEnable = yes
scroll1.showHorizontalScrollIndicator = false
scroll1.showVerticalScrollIndicator = false
scroll1.frame = CGRect(x: 0, y: 20, width: self.view.frame.size.width, height: self.view.frame.size.height-20)
scroll1.contentSize = CGSize(width: self.view.frame.size, 1640)
let session = AVSession()
session.setActive(true, options: [])
session.setCategory(.playback)
self.becomeFirstResponder()
}
override func viewWillAppear(_ animate: Bool){
self.becomeFirstResponder()
}
override func viewDidAppear(_ animate: Bool){
self.becomeFirstResponder()
let application = UIApplication.shared;
application.endReceivingRemoteControlEvents()
}
override func viewWillDisAppear(_ animate: Bool){
self.becomeFirstResponder()
}
override func viewDidDisAppear(_ animate: Bool){
self.becomeFirstResponder()
let application = UIApplication.shared;
application.beginReceivingRemoteControlEvents()
}
override func remoteControlReceived(with event: UIEvent){
switch event.subtype {
case .remoteControlPlay:
controller.player?.play()
case .remoteControlStop:
ii += 1
controller.player?.play()
case .remoteControlPause:
controller.player?.pause()
}
}
@IBAction func button1(_ sender: UIButton){
ii = 0
playVideo()
}
@IBAction func button2(_ sender: UIButton){
ii = 1
playVideo()
}
@IBAction func button3(_ sender: UIButton){
ii = 2
playVideo()
}
@IBAction func button4(_ sender: UIButton){
ii = 3
playVideo()
}
@IBAction func button5(_ sender: UIButton){
ii = 4
playVideo()
}
func playVideo(){
let label1 = UILabel()
label1.frame = CGRect(x: 0, y: self.view.frame.size.height-44, width: self.view.frame.size.width, height: 44)
label1.textColor = .gray
scroll1.backgroundColor = .black
label1.text = arr2[ii]
self.view.addSubView(label1)
let url = NSURL(string: arr[ii])! as URL
let playItem = AVPlayerItem(url: url)
player = AVPlayer(playerItem: playItem)
controller.player = player
controller.frame = CGRect(x: 0, y: 20, width: scroll1.frame.size.width, height: scroll1.frame.size.height/2-120)
controller.view.contentMode = .scaleAspectFit
self.view.addChild(controller)
self.view.addSubView(controller.view)
controller.player?.play()
NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying(ended:)), name: .AVPlayerItemDidPlayToEndTime, object: controller.player.currentItem)
}
@objc func playerDidFinishPlaying(ended: AVPlayer){
if ii > 5 {
ii = 0
controller.player?.pause()
} else {
ii += 1
playVideo()
}
}