Set up a player


To set up a player, use JWPlayerViewController. Like any UIViewController, JWPlayerViewController can be presented in multiple ways:

  • Presented directly (full screen)
  • Embedded in a container view (partial screen)

JWPlayerViewController uses the default JW Player user interface. If you subclass JWPlayerViewController, you can listen for all of the events related to the player and the user interface.


📘

Although the iOS SDK uses UIKit, the SDK can be used within SwiftUI.


Basic Player Implementation

The following code snippet demonstrates the following:

  • Subclassing JWPlayerViewController
  • Configuring the player with a single player item using JWPlayerConfigurationBuilder()
  • Overriding a player callback function to implement custom behavior
import UIKit
import JWPlayerKit

class CustomPlayerViewController: JWPlayerViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        do {
            // Create a JWPlayerItem
            let item = try JWPlayerItemBuilder()
                .file(URL(string:<#Video URL String#>)!)
                .build()

            // Create a config, and give it the item as a playlist.
            let config = try JWPlayerConfigurationBuilder()
                .playlist([item])
                .build()

            // Set the config
            player.configurePlayer(with: config)
        }
        catch {
            // Handle Error
        }
    }

    override func jwplayer(_ player: JWPlayer, didPauseWithReason reason: JWPauseReason) {
        super.jwplayer(player, didPauseWithReason: reason)
        // Implement custom behavior
    }
}
@interface CustomPlayerViewController: JWPlayerObjCViewController

@end

@implementation CustomPlayerViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Create a JWPlayerItem
    JWError *playerItemError;
    JWPlayerItemBuilder *playerItemBuilder = [[JWPlayerItemBuilder alloc] init];
    [playerItemBuilder file:[NSURL URLWithString:@"file.mp4"]];
    JWPlayerItem *playerItem = [playerItemBuilder buildAndReturnError:&playerItemError];
    if (playerItemError != nil) {
        // Handle error
    }

    // Create a config, and give it the item as a playlist.
    JWError *playerConfigError;
    JWPlayerConfigurationBuilder *playerConfigBuilder = [[JWPlayerConfigurationBuilder alloc] init];
    [playerConfigBuilder playlist:@[playerItem]];
    JWPlayerConfiguration *playerConfig = [playerConfigBuilder buildAndReturnError:&playerConfigError];
    if (playerConfigError != nil) {
        // Handle error
    }

    // Set the config
    [self.player configurePlayerWith:playerConfig];
}

- (void)jwplayer:(id<JWPlayer>)player didPauseWithReason:(enum JWPauseReason)reason
{

}

@end


Using Player APIs

Content Handling APIs

To use the player content handling APIs, the playerIsReady event must have already been initiated. The playerIsReady event is called once the player property of the JWPlayerViewController object has finished setup. Then, the event is sent to the JWPlayerDelegate.

📘

Unless set to another object, the JWPlayerDelegate is set to the JWPlayerViewController.


Values related to the currently loaded item such as player.currentItem will be set once the didLoadPlaylistItem delegate method is called.

// MARK: - JWPlayerDelegate

// Player is ready
override func jwplayerIsReady(_ player: JWPlayer) {
    super.jwplayerIsReady(player)
}

// Setup error, player is not ready
override func jwplayer(_ player: JWPlayer, failedWithSetupError code: UInt, message: String) {
    super.jwplayer(player, failedWithSetupError: code, message: message)
}
// Player is ready
- (void)jwplayerIsReady:(id<JWPlayer>)player
{
}

// Setup error, player is not ready
- (void)jwplayer:(id<JWPlayer>)player failedWithSetupError:(NSUInteger)code message:(NSString *)message
{
}

The content handling APIs are listed below:

FunctionsVariables
func forward(seconds: TimeInterval)

func getState() -> JWPlayerState

func loadPlayerItemAt(index: Int)

func loadPlaylist(_ playlist: [JWPlayerItem])

func next()

func pause()

func play()

func play(ad tag: URL, client: JWAdClient)

func previous()

func rewind(seconds: TimeInterval)

func seek(to seconds: TimeInterval)

func skipAd()

func stop()
var currentItem: JWPlayerItem? {get}

var playbackRate: Double { get set }

var time: JWTimeData { get }

var volume: Double { get set }

Non-content Handling APIs

APIs that do not relate to content handling can be accessed and used prior to the playerIsReady event being initiated.

The non-content handling APIs are listed in the following table.

FunctionEvent Delegates
func configurePlayer(with configuration: JWPlayerConfiguration)delegate: JWPlayerDelegate? { get set }

var contentKeyDataSource: JWDRMContentKeyDataSource? { get set }

var playbackStateDelegate: JWPlayerStateDelegate? { get set }

var adDelegate: JWAdDelegate? { get set }

var adTimeObserver: ((JWTimeData) -> Void)? { get set }

var airPlayDelegate: JWAirPlayDelegate? { get set }

var avDelegate: JWAVDelegate? { get set }

var friendlyObstructions: JWFriendlyObstructionManager { get }

var mediaTimeObserver: ((JWTimeData) -> Void)? { get set }

var metadataDelegates: JWMetadataDelegates { get }


Listening for Warnings and Errors

We recommend that you listen for possible errors and warnings thrown from the player through assigning a JWPlayerDelegate. Since the view controller in the previous code example conforms to the delegate, you only need to add the following code.

// MARK: - JWPlayerDelegate

    
    // Player is ready
    override func jwplayerIsReady(_ player: JWPlayer) {
        super.jwplayerIsReady(player)

    }

    // Setup error
    override func jwplayer(_ player: JWPlayer, failedWithSetupError code: UInt, message: String) {
        super.jwplayer(player, failedWithSetupError: code, message: message)

    }

    // Error
    override func jwplayer(_ player: JWPlayer, failedWithError code: UInt, message: String) {
        super.jwplayer(player, failedWithError: code, message: message)

    }

    // Warning
    override func jwplayer(_ player: JWPlayer, encounteredWarning code: UInt, message: String) {
        super.jwplayer(player, encounteredWarning: code, message: message)

    }

    // Ad error
    override func jwplayer(_ player: JWPlayer, encounteredAdError code: UInt, message: String) {
        super.jwplayer(player, encounteredAdError: code, message: message)

    }

    // Ad warning
    override func jwplayer(_ player: JWPlayer, encounteredAdWarning code: UInt, message: String) {
        super.jwplayer(player, failedWithSetupError: code, message: message)

    }
// Player is ready
- (void)jwplayerIsReady:(id<JWPlayer>)player
{
    
}

// Setup error
- (void)jwplayer:(id<JWPlayer>)player failedWithSetupError:(NSUInteger)code message:(NSString *)message
{

}

// Error
- (void)jwplayer:(id<JWPlayer>)player failedWithError:(NSUInteger)code message:(NSString *)message
{

}

// Warning
- (void)jwplayer:(id<JWPlayer>)player encounteredWarning:(NSUInteger)code message:(NSString *)message
{

}

// Ad error
- (void)jwplayer:(id<JWPlayer>)player encounteredAdError:(NSUInteger)code message:(NSString *)message
{

}

// Ad warning
- (void)jwplayer:(id<JWPlayer>)player encounteredAdWarning:(NSUInteger)code message:(NSString *)message
{

}


Did this page help you?