Set up a player


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

  • Presented directly
  • Embedded in a container view

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.



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: JWPlayerViewController

@end

@implementation CustomPlayerViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Create a JWPlayerItem
    JWError *playerItemError;
    JWPlayerItemBuilder *playerItemBulider = [[JWPlayerItemBuilder alloc] init];
    [playerItemBulider file:[NSURL URLWithString:@"file.mp4"]];
    JWPlayerItem *playerItem = [playerItemBulider 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


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?