Enabling playback of clear content

  1. In Xcode, create a new Single View project for your chosen platform (iOS/tvOS). In Project Options, select Swift as the language.

    The Player SDK can currently only be used in Objective C with a Swift wrapper.

  2. Add the OpenTV Player FPS SDK for iOS (OPYSDKFPS) or tvOS (OPYSDKFPSTv) to the project:

    • Copy the framework into the appropriate location (for example, the project root).
    • From the General tab, add the framework to 'Frameworks, Libraries, and Embedded Content', ensuring 'Embed & Sign' is selected. For example:

  3. In ViewController.swift, add the following import:

    import OPYSDKFPS // or OPYSDKFPSTv for tvOS
    
  4. Beneath the imports, add the code for our PlayerView class:

    class PlayerView: UIView {
    
        var player: AVPlayer? {
            get { return playerLayer.player }
            set { playerLayer.player = newValue }
        }
    
        var playerLayer: AVPlayerLayer {
            return layer as! AVPlayerLayer
        }
    
        override class var layerClass: AnyClass {
            return AVPlayerLayer.self
        }
    }
    
  5. Add members for the player and player view below the ViewController class definition:

    let otvPlayer: OTVAVPlayer
    @IBOutlet weak var playerView: PlayerView!
    
  6. Add the content URL:

    let assetURL = URL(string:
    "https://d3bqrzf9w11pn3.cloudfront.net/basic_hls_bbb_clear/index.m3u8")!
    
  7. Loading the OTVSDK object with the licence token. Before any other operation, the OTVSDK class must have one of its load() method called. Either:

    • Ensure the opy_licence file is present in the project and call the OTVSDK.load() method, OR
    • Call the OTVSDK.load(licence:) passing the licence as a String.

    In the example code, this call is added into the init method of the ViewController class so that it is always called when the object is created.

    load() will load the opy_licence file if it is included in the project. If the licence token is stored under a different file name, the string token must be loaded manually and passed in directly to OTVSDK.load(). Depending on the use case, it may be more suitable to call load() in the AppDelegate, however it must be called before the player is created.

    required init?(coder aDecoder: NSCoder) {
        OTVSDK.load()
    
        super.init(coder: aDecoder)
    }
    
  8. Instantiating an OTVAVPlayer

    Ensure SDK has been loaded before constructing the player.

    Initialise the player in the init method by passing in the asset url:

    required init?(coder aDecoder: NSCoder) {
        OTVSDK.load()
        otvPlayer = OTVAVPlayer(url: assetURL)
        super.init(coder: aDecoder)
    }
    

    The OTVAVPlayer takes one of the following:

    • an OTVAVPlayerItem object
    • the URL of a content stream.
  9. Inside the viewDidAppear method, assign our player to the playerView and start playback:

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        playerView.player = otvPlayer
        otvPlayer.play()
    }
    

    Your ViewController class should look like this:

    class ViewController: UIViewController {
    
        let otvPlayer: OTVAVPlayer
        @IBOutlet weak var playerView: PlayerView!
    
        let assetURL = URL(string:
            "https://d3bqrzf9w11pn3.cloudfront.net/basic_hls_bbb_clear/index.m3u8")!
    
        required init?(coder aDecoder: NSCoder) {
    
            OTVSDK.load() // provide licence string if necessary
            otvPlayer = OTVAVPlayer(url: assetURL)
            super.init(coder: aDecoder)
        }
    
        override func viewDidAppear(_ animated: Bool) {
            super.viewDidAppear(animated)
            playerView.player = otvPlayer
            otvPlayer.play()
        }
    }
    
  10. Link the view to the player. Open Main.storyboard and open the assistant editor. Drag from the small blue circle next to the IBOutlet that you added in ViewController.swift to the ViewController view, as follows:

  11. Build and run the app on your device. You should have clear playback.

    If you are experiencing difficulties, please refer to the 'basic playback' example application and check your application against that.

    Playback of FPS-encrypted content requires a few further steps and a FPS encrypted stream. These steps are detailed in the encrypted playback example.