0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

URLをArray() から取り出す方法 swift 編 

Last updated at Posted at 2021-04-01

#まずは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()
      } 
   }      
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?