iOS:使用媒體內容

在本主題中,您將學習如何使用 iOS 版原生 SDK 擷取和播放影片。

概覽

視訊雲端客戶可以從視訊雲端工作室存取其媒體資料和中繼資料。布萊特灣玩家的客戶將提供其媒體內容的 URL。

視訊雲端客戶

身為視訊雲端客戶,您可以存取儲存在視訊雲端工作室中的媒體資料。有關詳細信息,請參閱適用於 iOS 代碼示例的本機播放器 SDK .

擷取媒體資料

您可以使用播放 API,從視訊雲端媒體櫃擷取您的視訊和播放清單資料。有關 API 的詳細信息,請參閱播放 API 概述文檔。

  1. 使用BCOVPlaybackService類方法從 Brightcove 的播放 API 檢索您的視頻和播放列表。您的要求可以提供視頻/播放列表ID或者ReferenceID .此服務將發出 URL 請求並解析返回的數據。

  2. 對於此要求,您將需要一個原則金鑰。如果您不熟悉策略密鑰,請參閱策略 API 概述文檔。

    這是一個如何使用檢索視頻的示例BCOVPlaybackService斯威夫特類:

    import UIKit
    import BrightcovePlayerSDK
    
    let kViewControllerPlaybackServicePolicyKey = "your policy key"
    let kViewControllerAccountID = "your account id"
    let kViewControllerVideoID = "your video id"
    
    class ViewController: UIViewController, BCOVPlaybackControllerDelegate {
    
    let sharedSDKManager = BCOVPlayerSDKManager.shared()
    let playbackService = BCOVPlaybackService(accountId: kViewControllerAccountID, policyKey: kViewControllerPlaybackServicePolicyKey)
    let playbackController :BCOVPlaybackController
    @IBOutlet weak var videoContainerView: UIView!
    
    required init?(coder aDecoder: NSCoder) {
      playbackController = (sharedSDKManager?.createPlaybackController())!
    
      super.init(coder: aDecoder)
    
      playbackController.delegate = self
      playbackController.isAutoAdvance = true
      playbackController.isAutoPlay = true
    }
    
    override func viewDidLoad() {
      super.viewDidLoad()
      // Do any additional setup after loading the view, typically from a nib.
    
      // Set up our player view. Create with a standard VOD layout.
      guard let playerView = BCOVPUIPlayerView(playbackController: self.playbackController, options: nil, controlsView: BCOVPUIBasicControlView.withVODLayout()) else {
          return
      }
    
      // Install in the container view and match its size.
      self.videoContainerView.addSubview(playerView)
      playerView.translatesAutoresizingMaskIntoConstraints = false
      NSLayoutConstraint.activate([
        playerView.topAnchor.constraint(equalTo: self.videoContainerView.topAnchor),
        playerView.rightAnchor.constraint(equalTo: self.videoContainerView.rightAnchor),
        playerView.leftAnchor.constraint(equalTo: self.videoContainerView.leftAnchor),
        playerView.bottomAnchor.constraint(equalTo: self.videoContainerView.bottomAnchor)
      ])
    
      // Associate the playerView with the playback controller.
      playerView?.playbackController = playbackController
    
      requestContentFromPlaybackService()
    }
    
    func requestContentFromPlaybackService() {
      playbackService?.findVideo(withVideoID: kViewControllerVideoID, parameters: nil) { (video: BCOVVideo?, jsonResponse: [AnyHashable: Any]?, error: Error?) -> Void in
    
        if let v = video {
        	self.playbackController.setVideos([v] as NSArray)
        } else {
        	print("ViewController Debug - Error retrieving video: \(error?.localizedDescription ?? "unknown error")")
        }
      }
    }
    }

    如需完整範例,請參閱下列內容:

  3. BCOV視頻對象提供視頻元數據信息,如下所示:

    func requestContentFromPlaybackService() {
    playbackService?.findVideo(withVideoID: kViewControllerVideoID, parameters: nil) { (video: BCOVVideo?, jsonResponse: [AnyHashable: Any]?, error: Error?) -> Void in
    
      if let v = video {
    	print("video name: \(v.properties["name"] as AnyObject)")
      	print("video id: \(v.properties["id"] as AnyObject)")
      	print("video thumbnail: \(v.properties["thumbnail"] as AnyObject)")
      	print("video metadata: \(v.properties)")
    
      	self.playbackController.setVideos([v] as NSArray)
      } else {
      	print("ViewController Debug - Error retrieving video: \(error?.localizedDescription ?? "unknown error")")
      }
    }
    }

    以上print()方法返回以下媒體信息:

    視訊物件
    視訊物件

處理經過地理過濾的視頻

適用於iOS的Brightcove Player SDK支持經過地理過濾的視頻。

您可以通過兩種方法向視頻添加地理過濾,以控制可以(或不能)在哪些國家/地區觀看視頻:

在您的 iOS 應用程序中,當您使用 Brightcove 的檢索視頻時BCOVPlaybackService類(播放 API)在針對該視頻進行地理過濾的國家/地區中,您應該會看到此消息:

Error Domain=kBCOVPlaybackServiceErrorDomain Code=3 "(null)"
UserInfo={kBCOVPlaybackServiceErrorKeyAPIHTTPStatusCode=403,
kBCOVPlaybackServiceErrorKeyAPIErrors=
{type = immutable, count = 1, values = (
0 : {type = immutable dict, count = 4,
entries =>
1 : message = {contents = "Access to this resource is forbidden by access policy."}
2 : {contents = "client_geo"} = us
4 : {contents = "error_subcode"} = {contents = "CLIENT_GEO"}
6 : {contents = "error_code"} = {contents = "ACCESS_DENIED"}
}
)}}

布萊特灣玩家顧客

身為布萊特灣播放器客戶,您將提供視訊資產的網址。

以下是將視頻陣列添加到播放控制器並開始播放的示例:

import UIKit
import BrightcovePlayerSDK

let kViewControllerAccountID = "your account id" // For Brightcove registration

class ViewController: UIViewController, BCOVPlaybackControllerDelegate {
  let sharedSDKManager = BCOVPlayerSDKManager.shared()
  let playbackController :BCOVPlaybackController
  @IBOutlet weak var videoContainerView: UIView!

  required init?(coder aDecoder: NSCoder) {
    // Create the Brightcove playback controller
    playbackController = (sharedSDKManager?.createPlaybackController())!

    super.init(coder: aDecoder)

    // Register your app with Brightcove
    playbackController.analytics.account = kViewControllerAccountID

    // Configure the player
    playbackController.delegate = self
    playbackController.isAutoAdvance = true
    playbackController.isAutoPlay = true
  }

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    // Create an array of videos
    var videoArray = [AnyObject]()
    videoArray = [videoWithURL(url: NSURL(string: "https://solutions.brightcove.com/bcls/assets/videos/Great_Horned_Owl.mp4")!),
                  videoWithURL(url: NSURL(string: "https://solutions.brightcove.com/bcls/assets/videos/Great_Blue_Heron.mp4")!)]

    // Set up the player view with a standard VOD layout.
    guard let playerView = BCOVPUIPlayerView(playbackController: self.playbackController, options: nil, controlsView: BCOVPUIBasicControlView.withVODLayout()) else {
      return
    }

    // Install in the container view and match its size.
    self.videoContainerView.addSubview(playerView)
    playerView.translatesAutoresizingMaskIntoConstraints = false
    NSLayoutConstraint.activate([
      playerView.topAnchor.constraint(equalTo: self.videoContainerView.topAnchor),
      playerView.rightAnchor.constraint(equalTo: self.videoContainerView.rightAnchor),
      playerView.leftAnchor.constraint(equalTo: self.videoContainerView.leftAnchor),
      playerView.bottomAnchor.constraint(equalTo: self.videoContainerView.bottomAnchor)
    ])

    // Associate the playerView with the playback controller.
    playerView.playbackController = playbackController

    // Load the video array into the player and start video playback
    playbackController.setVideos(videoArray as NSArray)
    playbackController.play();
  }

  func videoWithURL(url: NSURL) -> BCOVVideo {
    // Set the delivery method for BCOVSources that belong to a video
    let source:BCOVSource = BCOVSource(url: url as URL, deliveryMethod: kBCOVSourceDeliveryHLS, properties: nil)
    let video = BCOVVideo.init(source: source, cuePoints: BCOVCuePointCollection.init(array: []), properties: [NSObject:AnyObject]())
    return video!
  }
}

如需完整範例,請參閱下列內容:

您現在對於 iOS 的布萊特灣播放器 SDK 有一個基本的了解。接下來,您將逐步完成構建使用Player SDK的應用的步驟