快速循序渐进?

时间:2018-08-29 12:19:16

标签: swift

我有以下带有网址的数组:

let KStorePlayURL = [
    https://source.s3-us-west-2.amazonaws.com/ENVOI/2018/07/19/ATASTEOFDANCE_S1_EP3.mp4,                
    https://source.s3-us-west-2.amazonaws.com/ENVOI/2018/05/23/ATasteOfDance_S1E1_Episode.mp4,                         
    https://source.s3-us-west-2.amazonaws.com/ENVOI/2018/05/23/ATasteOfDance_S1E1_Episode.mp4,
    https://source.s3-us-west-2.amazonaws.com/ENVOI/2018/05/23/ATasteOfDance_S1E1_Episode.mp4
] 

如何在for循环中一个接一个地获取URL?还可以在此Bitmovin播放器中播放一个视频,然后播放其他视频吗?

这是我已经尝试过的代码:

@objc func setUpPlayerVideos1() {

   print(KStorePlayURL)

   for i in 0..<KStorePlayURL.count {

      let streamURL = URL(string: KStorePlayURL[i])

      playlist.append(PlaylistItem(url: streamURL!, title: "player"))
      // Create player based with a default configuration
      let player = BitmovinPlayer()

      // Create player view and pass the player instance to it
      let playerView = BMPBitmovinPlayerView(player: player, frame: .zero)

      // Listen to player events
      player.add(listener: self)

      playerView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
      playerView.frame = view.bounds

      view.addSubview(playerView)
      view.bringSubview(toFront: playerView)

      // store the reference to the player
      self.Bitmovinplayer = player

   }
   // Start the playlist
   playNextItem()
}

1 个答案:

答案 0 :(得分:0)

您不应该使用for循环,因为您将在每次迭代中覆盖播放器。您需要做的是使用for循环或更实用的样式填充playlist

KStorePlayURL.forEach { urlString in
    //Make sure that the url address is correct
    guard let streamURL = URL(string: urlString) else {
        fatalError("Error in stream url")
    }
    playlist.append(PlaylistItem(url: streamURL!, title: "player"))
}

这里我假设KStorePlayURL是一个字符串数组:

let KStorePlayURL = [
    "https://source.s3-us-west-2.amazonaws.com/ENVOI/2018/07/19/ATASTEOFDANCE_S1_EP3.mp4",
    "https://source.s3-us-west-2.amazonaws.com/ENVOI/2018/05/23/ATasteOfDance_S1E1_Episode.mp4",
    "https://source.s3-us-west-2.amazonaws.com/ENVOI/2018/05/23/ATasteOfDance_S1E1_Episode.mp4",
    "https://source.s3-us-west-2.amazonaws.com/ENVOI/2018/05/23/ATasteOfDance_S1E1_Episode.mp4"
]

您的最终代码应如下所示:

@objc func setUpPlayerVideos1() {

    KStorePlayURL.forEach { urlString in
        guard let streamURL = URL(string: urlString) else {
            fatalError("Error in stream url")
        }
        playlist.append(PlaylistItem(url: streamURL!, title: "player"))
    }

        let player = BitmovinPlayer()

        // Create player view and pass the player instance to it
        let playerView = BMPBitmovinPlayerView(player: player, frame: .zero)

        // Listen to player events
        player.add(listener: self)

        playerView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        playerView.frame = view.bounds

        view.addSubview(playerView)
        view.bringSubview(toFront: playerView)

        // store the reference to the player
        self.Bitmovinplayer = player

    }
    // Start the playlist
    playNextItem()
}

您可以找到用于通过bitmovin播放器here播放播放列表的完整示例代码。