CONNECT Player SDK 5 for Apple (FPS)
5.14.x 5.13.x 5.12.x 5.11.x 5.10.x 5.9.x 5.8.x 5.7.x 5.6.x 5.5.x 5.4.x 5.3.x 5.2.x 5.1.x 5.0.x
5.14.x 5.13.x 5.12.x 5.11.x 5.10.x 5.9.x 5.8.x 5.7.x 5.6.x 5.5.x 5.4.x 5.3.x 5.2.x 5.1.x 5.0.x

Creating the player

Use the following procedure to create a new Apple iOS or tvOS project to build the basic player for app development using Swift. 

Alternative example code is provided for Objective-C development (not described here).

  1. Unzip the opy-sdk-ios-fps-5.1.x-integration.zip or opy-sdk-tvos-fps-5.1.x-integration.zip file, and copy the OPYSDKFPS.framework or OPYSDKFPSTv.framework file to an appropriate location (for example, the project root).

  2. In Xcode, create a new Single View project for your iOS/tvOS platform.

    • In project options, select Swift as the language.

    • On the General tab, add the framework to Frameworks, Libraries, and Embedded Content, ensuring Embed & Sign is selected.

  3. To connect to non-HTTPS URLs, add Application Transport Security (ATS) settings to info.plist. Open the info.plist file and add an App Transport Security entry for your content (replacing  www.example.com  with the actual URL):

    XML
        <key>NSAppTransportSecurity</key>
          <dict>
          <key>NSExceptionDomains</key>
              <dict>
              <key>www.example.com</key>
                <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
              </dict>
            </dict>
          </dict>
    
  4. Add the following import in ViewController.swift .

    AppleScript
    import OPYSDKFPS // or OPYSDKFPSTv for tvOS
    

    Below the imports, add the code for the PlayerView class:

    AppleScript
        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.

    AppleScript
        let otvPlayer: OTVAVPlayer
        @IBOutlet weak var playerView: PlayerView!
    

    Add the content URL:

    AppleScript
        let assetURL = URL(string:
        "https://d3bqrzf9w11pn3.cloudfront.net/basic_hls_bbb_clear/index.m3u8")!
    
  6. Instantiate an OTVAVPlayer

    Ensure SDK has been loaded before constructing the player.

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

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

    The OTVAVPlayer takes either an OTVAVPlayerItem object or the URL of a content stream.

  7. Inside the  viewDidAppear  method, assign our player to the playerView and start playback:

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

    Your ViewController class should look like the following:

    AppleScript
    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()
                otvPlayer = OTVAVPlayer(url: assetURL)
                super.init(coder: aDecoder)
            }
    
            override func viewDidAppear(_ animated: Bool) {
                super.viewDidAppear(animated)
                playerView.player = otvPlayer
                otvPlayer.play()
            }
        }
    
  8. 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:

IBOutlet_1.png

Next step: Run the app in clear playback mode.