迭代视频

时间:2019-06-14 18:45:56

标签: swift video avplayer

我正在尝试让播放器遍历引用视频URL的字符串数组。如果我在代码中对它进行硬编码,我只能让它播放阵列中的视频之一,但是我不能这样做来进行迭代。

我已经开始播放一部视频,虽然它是硬编码但不是重复的。

   var playerArray = [AVQueuePlayer]()
    var player = AVQueuePlayer()
   // var URLSArray = [URL]()
    var ItemArray = [AVPlayerItem]()

    var videos = ["video1", "video2","video3"]

    var playerController = AVPlayerViewController()




override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let videoNum = (self.videos.count-1)

  //  var URLSArray = [URL]()

 //   for n in 0...videoNum{
 //       let pathString = (Bundle.main.path(forResource: videos[n], ofType: "mov"))
  //      let StrToURL  = NSURL(string: pathString!)
  //      URLSArray.append(StrToURL! as URL)

 //   }

    var ItemArray = [AVPlayerItem]()
 //  var playerController = AVPlayerViewController()

    for n  in 0...videoNum{
        let pathString = (Bundle.main.path(forResource: videos[n], ofType: "mov"))

        //print(pathString!)
        let StrToURL  = URL(string: pathString!)
       // print(StrToURL!)
       // ItemArray = [AVPlayerItem(url: StrToURL!)]
        ItemArray.append(AVPlayerItem(url:StrToURL!))
        print(n)
        print(ItemArray)
       // let player = AVPlayer(playerItem: ItemArray[0])

        player = AVQueuePlayer(items: [ItemArray[n]])


        player.play()
                   }




             //  playerController.player = player
              // present(playerController, animated: true) {
                //   player.play()

    }

我希望它完成一个视频的播放并移至数组中的下一个视频,我如何从该数组中对视频进行硬编码,但是如果我要进行迭代,就好像URL不是工作。这就是它所说的2019-06-14 13:08:44.718684-0400 Video [5439:1288491] NSURLConnection已完成,发生错误-代码-1002

1 个答案:

答案 0 :(得分:0)

仅声明玩家

var player = AVQueuePlayer()

viewDidAppear中,将字符串数组映射到应用程序捆绑包中的URL,然后将URL映射到播放器项。然后创建排队的播放器并开始播放。

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let videos = ["video1", "video2", "video3"]
    let videoURLs = videos.compactMap{ Bundle.main.url(forResource: $0, withExtension: "mov")}
    let itemArray = videoURLs.map{ AVPlayerItem(url: $0) }

    player = AVQueuePlayer(items: itemArray)
    player.play()

    //  playerController.player = player
    // present(playerController, animated: true) {
    //   player.play()
    // }
}