Examples

General example

All examples below differs in sample ViewController::viewDidLoad() method and sometimes ViewController implements some methods for acting as delegate for player. Below is presented complete example with full file content, but for next examples only changes in ViewController::viewDidLoad() are present.

BradmaxPlayer class extends WkWebView component, so can be threaded as WkWebView with additional interface for player control.

iOS ViewController.h header file

#import <UIKit/UIKit.h>
#import "BPSPlayerDelegate.h"
#import "ViewController.h"

@interface ViewController : UIViewController <BPSPlayerDelegate>

@end

iOS ViewController.m source file

#import "ViewController.h"
#import "BPSMAdMedia.h"
#import "BPSMAdvertisement.h"
#import "BPSMAdvertisementAdMedia.h"
#import "BPSMAdvertisementBreak.h"
#import "BPSMMedia.h"
#import "BPSMMediaPlaybackState.h"
#import "BPSMMediaSource.h"
#import "BPSMPlayerConfig.h"
#import "BPSMSplashImage.h"
#import "BPSMSubtitlesFile.h"
#import "BPSPlayer.h"
#import "BPSPlayerDelegate.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    BPSPlayer *player = [[BPSPlayer alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:player];
    [player loadVideoByUrl:@"https://bradmax.com/static/video/tos/big_buck_bunny.m3u8"
        withPosterUrl:@"https://bradmax.com/static/images/startsplash.jpg"];
}

@end

Player loaded from file.

This example shows how to use custom configured and generated bradmax JavaScript player from bradmax playform. For using it:

  1. You have to download player build from https://bradmax.com/client/panel/ ,
  2. Extract bradmax_player.js file from downloaded ZIP file ,
  3. Save bradmax_player.js file in your sources file directory (main app bundle directory).
// EXAMPLE: Player loaded from local bradmax_player.js file embedded in application (customized player in file).
BPSPlayer *player = [[BPSPlayer alloc] initWithFrame:self.view.bounds];
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *localBradmaxPlayerJsFilePath = [mainBundle pathForResource:@"bradmax_player" ofType:@"js"];
player.localBradmaxPlayerJs = [NSString stringWithContentsOfFile:localBradmaxPlayerJsFilePath
                                              encoding:NSUTF8StringEncoding
                                                 error:NULL];
[player loadVideoByUrl:@"https://bradmax.com/static/video/tos/big_buck_bunny.m3u8"
         withPosterUrl:@"https://bradmax.com/static/images/startsplash.jpg"];

Note: bradmax_player.js is loaded from mainBundle as (NSString *) into player.localBradmaxPlayerJs player property. When this property is set, then instead of using default embedded player custom one is used.

Player loaded remotely from bradmax

In this example player is always loading from bradmax platform. If player didn’t change on bradmax platform since last load then it is loaded from WkWebView cache. When player view or configuration is changed on bradmax platform (eg. adding custom colours and logo for holidays), then new view will be downloaded next time player is opened.

For using remotly loaded player “embed Id” is needed. For obraining it please:

  1. Open https://bradmax.com/client/panel/ and and sign-in.
  2. Open players list page: https://bradmax.com/client/panel/admin/player/list
  3. Click “get embed code” button on selected player.
  4. Check iframe embed code. It should looks like
<iframe src="https://bradmax.com/client/embed-player/6e6040e800a877db910c71d42e95980e7787eceb_4023?mediaUrl=https%3A%2F%2Fbradmax.com%2Fstatic%2Fvideo%2Ftears_of_steel.mp4&title=Tears%20of%20steel&duration=734.097415&splashImgUrl=https%3A%2F%2Fbradmax.com%2Fstatic%2Fimages%2Fstartsplash.jpg" width="600" height="400" frameBorder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>

In iframe src attribute contain URL. Search there for "embed id", which is placed "https://bradmax.com/client/embed-player/[EMBED_ID]?mediaUrl=…". It is just after “/embed-player/” and before "?mediaUrl=".

  1. Use “embed id” in BradmaxPlayer.setRemoteBradmaxPlayerEmbedId() method.

Example:

// EXAMPLE: Player loaded remotely from bradmax.
BPSPlayer *player = [[BPSPlayer alloc] initWithFrame:self.view.bounds];
player.remoteBradmaxPlayerEmbedId = @"6e6040e800a877db910c71d42e95980e7787eceb_4023";
[player loadVideoByUrl:@"https://bradmax.com/static/video/tos/big_buck_bunny.m3u8"
         withPosterUrl:@"https://bradmax.com/static/images/startsplash.jpg"];

Defining playlist with one video with subtitles

Sample below show how to define playlist. Playlist below contains two videos. First use minimal settings - just defining duration, so before starting playback it will be displayed correctly on player start splash screen. Second use single external file for subtitles in Czech language (“cz” - language code).

// EXAMPLE: Defining playlist with one video with subtitles.
BPSPlayer *player = [[BPSPlayer alloc] initWithFrame:self.view.bounds];

// Defining first video (BPSMMedia *m1).
BPSMMedia *m1 = [BPSMMedia newWithMediaSource:@"https://bradmax.com/static/video/tos/big_buck_bunny.m3u8"
                              withSplashImage:@"https://bradmax.com/static/images/startsplash.jpg"];
m1.duration = [NSNumber numberWithDouble:596.0];
[player addMediaToPlayback:m1];

// Defining second video (BPSMMedia *m2).
BPSMSubtitlesFile *subtitlesLangCz = [BPSMSubtitlesFile newWithLanguageCode:@"cz" withUrl:@"https://bradmax.com/static/video/tos/tesla/tesla_cz.srt"];
BPSMMedia *m2 = [BPSMMedia newWithMediaSource:@"https://bradmax.com/static/video/tos/tesla/tesla.m3u8"];
m2.duration = [NSNumber numberWithDouble:266.0];
m2.subtitlesSets = [NSMutableArray new];
[m2.subtitlesSets addObject:subtitlesLangCz];
[player addMediaToPlayback:m2];

player.playerConfig.subtitles = @"cz";

// Calling player to load, when player playlist and configuration was prepared.
[player load];

Setting

player.playerConfig.subtitles = @"cz";

choose Czech (“cz” - ISO language code) language as default. If any media got Czech subtitles, then by default these will be enabled and displayed during media playback.

Implementing player delegate

Example below shows how to implement player delegate, which will perform some action on player playback event.

// EXAMPLE: Implementing player delegate.
BPSMMedia *m1 = [BPSMMedia newWithMediaSource:@"https://bradmax.com/static/video/tos/big_buck_bunny.m3u8"
                              withSplashImage:@"https://bradmax.com/static/images/startsplash.jpg"];
m1.id = @"MEDIA_ID_1";
m1.title = @"VIDEO 1";
m1.duration = [NSNumber numberWithDouble:596.0];
[player addMediaToPlayback:m1];

BPSMSubtitlesFile *subtitlesLangCz = [BPSMSubtitlesFile newWithLanguageCode:@"cz" withUrl:@"https://bradmax.com/static/video/tos/tesla/tesla_cz.srt"];
BPSMMedia *m2 = [BPSMMedia newWithMediaSource:@"https://bradmax.com/static/video/tos/tesla/tesla.m3u8"];
m2.id = @"MEDIA_ID_2";
m2.title = @"VIDEO 2";
m2.duration = [NSNumber numberWithDouble:266.0];
m2.subtitlesSets = [NSMutableArray new];
[m2.subtitlesSets addObject:subtitlesLangCz];
[player addMediaToPlayback:m2];

player.playerConfig.subtitles = @"cz";
player.playerDelegate = self;
[player load];

Methods for adding into controler

// Method called, when player loads.
- (void) bpsPlayerDidPlayerLoad {
    NSLog(@"bpsPlayerDidPlayerLoad");
}

// Method called, when video starts playing.
- (void) bpsPlayerDidVideoPlaying:(BPSMMediaPlaybackState *)mediaPlaybackState {
    NSLog(@"bpsPlayerDidVideoPlaying playback currentTime:%@ duration:%@", mediaPlaybackState.currentTime, mediaPlaybackState.duration);
    if(mediaPlaybackState.media != nil) {
        NSLog(@"bpsPlayerDidVideoPlaying media id:%@ title:%@", mediaPlaybackState.media.id, mediaPlaybackState.media.title);
        if(mediaPlaybackState.media.subtitlesSets != nil) {
            for(BPSMSubtitlesFile *fileInfo in mediaPlaybackState.media.subtitlesSets) {
                NSLog(@"bpsPlayerDidVideoPlaying media.subtitles languageCode:%@", fileInfo.languageCode);
            }
        }
    }
}

// Method called, when video is paused by user.
- (void) bpsPlayerDidVideoPaused:(BPSMMediaPlaybackState *)mediaPlaybackState {
    NSLog(@"bpsPlayerDidVideoPaused mediaPlaybackState currentTime:%@ duration:%@", mediaPlaybackState.currentTime, mediaPlaybackState.duration);
}

// Method called, when video playback time is updated (method periodically called during normal playback). 
- (void) bpsPlayerDidVideoCurrentTimeChange:(BPSMMediaPlaybackState *)mediaPlaybackState {
    NSLog(@"bpsPlayerDidVideoCurrentTimeChange mediaPlaybackState currentTime:%@ duration:%@", mediaPlaybackState.currentTime, mediaPlaybackState.duration);
}

// Method called, when next video from playlist is loaded into player.
- (void) bpsPlayerDidDataProviderMediaMetadataData:(BPSMMedia *)media {
    NSLog(@"bpsPlayerDidDataProviderMediaMetadataData media id:%@ title:%@ duration:%@", media.id, media.title, media.duration);
}

For full list for supported events please check BPSPlayerDelegate protocol methods on https://bradmax.com/static/bradmax-ios-sdk/latest/doxygen/ .

Simple Ads example with loading ads from remote VMAP file

IMPORTANT NOTE: Enabling Advertisement plugin is required in player configurator. Advertisement plugin is not available in free plan.

Activation of Advertisement plugin:

  1. Open players List (https://bradmax.com/client/panel/admin/player/list)
  2. Select your player
  3. Click on “Advertisement plugins”
  4. Click on “VAST/VMAP” switch for changing to "enable".
// EXAMPLE: Simple Ads example with loading ads from remote VMAP file.
// IMPORTANT NOTE: Enabling Advertisement plugin is required in player configurator.
//                 Players List (https://bradmax.com/client/panel/admin/player/list)
//                   > Select your player
//                   > Advertisement plugins
//                   > VAST/VMAP (enable)

NSBundle *mainBundle = [NSBundle mainBundle];
NSString *localBradmaxPlayerJsFilePath = [mainBundle pathForResource:@"bradmax_player" ofType:@"js"];
player.localBradmaxPlayerJs = [NSString stringWithContentsOfFile:localBradmaxPlayerJsFilePath
encoding:NSUTF8StringEncoding
error:NULL];

BPSMMedia *m1 = [BPSMMedia newWithMediaSource:@"https://bradmax.com/static/video/tos/big_buck_bunny.m3u8"
    withSplashImage:@"https://bradmax.com/static/images/startsplash.jpg"];
m1.id = @"MEDIA_ID_1";
m1.title = @"MEDIA_TITLE_1";
m1.duration = [NSNumber numberWithDouble:596.0];
[player addMediaToPlayback:m1];
player.playerConfig.advertisement = [BPSMAdvertisement new];
player.playerConfig.advertisement.vmap = @"https://bradmax.com/static/a/vmap.3.xml";
player.playerDelegate = self;
[player load];