Skip to content

Commit 7e95e96

Browse files
committedMay 27, 2019
feat: Implements poster and playlist support on ios
1 parent 6437562 commit 7e95e96

12 files changed

+346
-34
lines changed
 

‎index.d.ts

+40-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { ViewStyle } from 'react-native';
33

44
type VideoToken = string;
55

6-
export type Props = {
7-
policyKey: string;
8-
accountId: string;
6+
export type BrightcovePlayerProps = {
7+
policyKey?: string;
8+
accountId?: string;
99
referenceId?: string;
1010
videoId?: string;
1111
videoToken?: VideoToken;
@@ -28,11 +28,37 @@ export type Props = {
2828
style?: ViewStyle;
2929
};
3030

31-
export class BrightcovePlayer extends React.Component<Props, {}> {
31+
export class BrightcovePlayer extends React.Component<
32+
BrightcovePlayerProps,
33+
{}
34+
> {
3235
seekTo(position: number): {};
3336
}
3437

38+
export type BrightcovePlayerPosterProps = {
39+
policyKey?: string;
40+
accountId?: string;
41+
referenceId?: string;
42+
videoId?: string;
43+
videoToken?: VideoToken;
44+
style?: ViewStyle;
45+
};
46+
47+
export class BrightcovePlayerPoster extends React.Component<
48+
BrightcovePlayerPosterProps,
49+
{}
50+
> {}
51+
3552
export namespace BrightcovePlayerUtil {
53+
type PlaylistVideo = {
54+
accountId: String;
55+
videoId: String;
56+
referenceId: String;
57+
name: String;
58+
description: String;
59+
duration: number;
60+
};
61+
3662
export function requestDownloadVideoWithReferenceId(
3763
accountId: string,
3864
policyKey: string,
@@ -64,4 +90,14 @@ export namespace BrightcovePlayerUtil {
6490
videoToken: VideoToken;
6591
}[]
6692
>;
93+
export function getPlaylistWithReferenceId(
94+
accountId: string,
95+
policyKey: string,
96+
referenceId: string
97+
): Promise<PlaylistVideo[]>;
98+
export function getPlaylistWithPlaylistId(
99+
accountId: string,
100+
policyKey: string,
101+
playlistId: string
102+
): Promise<PlaylistVideo[]>;
67103
}

‎index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const BrightcovePlayer = require('./src/BrightcovePlayer');
2+
const BrightcovePlayerPoster = require('./src/BrightcovePlayerPoster');
23
const BrightcovePlayerUtil = require('./src/BrightcovePlayerUtil');
34

45
module.exports = {
56
BrightcovePlayer: BrightcovePlayer,
7+
BrightcovePlayerPoster: BrightcovePlayerPoster,
68
BrightcovePlayerUtil: BrightcovePlayerUtil
79
};

‎ios/BrightcovePlayer.h

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
@property (nonatomic) float targetVolume;
2424
@property (nonatomic) float targetBitRate;
2525
@property (nonatomic) float targetPlaybackRate;
26+
@property (nonatomic) BOOL playbackServiceDirty;
2627

2728
@property (nonatomic, copy) NSString *referenceId;
2829
@property (nonatomic, copy) NSString *videoId;

‎ios/BrightcovePlayer.m

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ - (void)setup {
3131
}
3232

3333
- (void)setupService {
34-
if (!_playbackService && _accountId && _policyKey) {
34+
if ((!_playbackService || _playbackServiceDirty) && _accountId && _policyKey) {
35+
_playbackServiceDirty = NO;
3536
_playbackService = [[BCOVPlaybackService alloc] initWithAccountId:_accountId policyKey:_policyKey];
3637
}
3738
}
@@ -87,12 +88,14 @@ - (void)setVideoToken:(NSString *)videoToken {
8788

8889
- (void)setAccountId:(NSString *)accountId {
8990
_accountId = accountId;
91+
_playbackServiceDirty = YES;
9092
[self setupService];
9193
[self loadMovie];
9294
}
9395

9496
- (void)setPolicyKey:(NSString *)policyKey {
9597
_policyKey = policyKey;
98+
_playbackServiceDirty = YES;
9699
[self setupService];
97100
[self loadMovie];
98101
}

‎ios/BrightcovePlayerPoster.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#import <UIKit/UIKit.h>
2+
#import <BrightcovePlayerSDK/BCOVPlayerSDKManager.h>
3+
#import <BrightcovePlayerSDK/BCOVPlaybackService.h>
4+
#import <React/RCTBridge.h>
5+
#import <React/UIView+React.h>
6+
7+
@interface BrightcovePlayerPoster : UIImageView
8+
9+
@property (nonatomic) BCOVPlaybackService *playbackService;
10+
@property (nonatomic) BOOL playbackServiceDirty;
11+
12+
@property (nonatomic, copy) NSURLSession *session;
13+
@property (nonatomic, copy) NSString *referenceId;
14+
@property (nonatomic, copy) NSString *videoId;
15+
@property (nonatomic, copy) NSString *videoToken;
16+
@property (nonatomic, copy) NSString *accountId;
17+
@property (nonatomic, copy) NSString *policyKey;
18+
19+
@end

‎ios/BrightcovePlayerPoster.m

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#import "BrightcovePlayerPoster.h"
2+
#import "BrightcovePlayerOfflineVideoManager.h"
3+
4+
@interface BrightcovePlayerPoster ()
5+
@end
6+
7+
@implementation BrightcovePlayerPoster
8+
9+
- (instancetype)initWithFrame:(CGRect)frame {
10+
if (self = [super initWithFrame:frame]) {
11+
self.contentMode = UIViewContentModeScaleAspectFill;
12+
self.clipsToBounds = YES;
13+
}
14+
return self;
15+
}
16+
17+
- (void)setReferenceId:(NSString *)referenceId {
18+
_referenceId = referenceId;
19+
_videoId = NULL;
20+
[self setupService];
21+
[self loadPoster];
22+
}
23+
24+
- (void)setVideoId:(NSString *)videoId {
25+
_videoId = videoId;
26+
_referenceId = NULL;
27+
[self setupService];
28+
[self loadPoster];
29+
}
30+
31+
- (void)setVideoToken:(NSString *)videoToken {
32+
_videoToken = videoToken;
33+
[self loadPoster];
34+
}
35+
36+
- (void)setAccountId:(NSString *)accountId {
37+
_accountId = accountId;
38+
_playbackServiceDirty = YES;
39+
[self setupService];
40+
[self loadPoster];
41+
}
42+
43+
- (void)setPolicyKey:(NSString *)policyKey {
44+
_policyKey = policyKey;
45+
_playbackServiceDirty = YES;
46+
[self setupService];
47+
[self loadPoster];
48+
}
49+
50+
- (void)setResizeMode:(NSString *)resizeMode {
51+
if ([resizeMode isEqualToString:@"contain"]) {
52+
self.contentMode = UIViewContentModeScaleAspectFit;
53+
} else if ([resizeMode isEqualToString:@"fit"]) {
54+
self.contentMode = UIViewContentModeScaleToFill;
55+
} else {
56+
self.contentMode = UIViewContentModeScaleAspectFill;
57+
}
58+
}
59+
60+
- (void)setupService {
61+
if ((!_playbackService || _playbackServiceDirty) && _accountId && _policyKey) {
62+
_playbackServiceDirty = NO;
63+
_playbackService = [[BCOVPlaybackService alloc] initWithAccountId:_accountId policyKey:_policyKey];
64+
}
65+
}
66+
67+
- (void)loadPoster {
68+
if (_videoToken) {
69+
BCOVVideo *video = [[BrightcovePlayerOfflineVideoManager sharedManager] videoObjectFromOfflineVideoToken:_videoToken];
70+
if (video) {
71+
self.image = [UIImage imageWithContentsOfFile:video.properties[kBCOVOfflineVideoPosterFilePathPropertyKey]];
72+
}
73+
return;
74+
}
75+
if (!_playbackService) return;
76+
if (_videoId) {
77+
[_playbackService findVideoWithVideoID:_videoId parameters:nil completion:^(BCOVVideo *video, NSDictionary *jsonResponse, NSError *error) {
78+
if (video) {
79+
[self loadImage:video.properties[kBCOVVideoPropertyKeyPoster]];
80+
}
81+
}];
82+
} else if (_referenceId) {
83+
[_playbackService findVideoWithReferenceID:_referenceId parameters:nil completion:^(BCOVVideo *video, NSDictionary *jsonResponse, NSError *error) {
84+
if (video) {
85+
[self loadImage:video.properties[kBCOVVideoPropertyKeyPoster]];
86+
}
87+
}];
88+
}
89+
}
90+
91+
-(void)loadImage:(NSString *)url{
92+
if (_session) {
93+
[_session invalidateAndCancel];
94+
}
95+
if (!url) return;
96+
_session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]
97+
delegate:nil
98+
delegateQueue:[NSOperationQueue mainQueue]];
99+
NSURLSessionTask *task = [_session dataTaskWithURL:[NSURL URLWithString:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
100+
if (error || !data) {
101+
return;
102+
}
103+
self.image = [UIImage imageWithData:data];
104+
}];
105+
[task resume];
106+
}
107+
108+
@end

‎ios/BrightcovePlayerPosterManager.h

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#import <React/RCTViewManager.h>
2+
3+
@interface BrightcovePlayerPosterManager : RCTViewManager
4+
5+
@end

‎ios/BrightcovePlayerPosterManager.m

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#import "BrightcovePlayerPosterManager.h"
2+
#import "BrightcovePlayerPoster.h"
3+
#import <React/RCTUIManager.h>
4+
5+
@implementation BrightcovePlayerPosterManager
6+
7+
RCT_EXPORT_MODULE();
8+
9+
@synthesize bridge = _bridge;
10+
11+
- (UIView *)view {
12+
return [[BrightcovePlayerPoster alloc] init];
13+
}
14+
15+
- (dispatch_queue_t)methodQueue {
16+
return _bridge.uiManager.methodQueue;
17+
}
18+
19+
RCT_EXPORT_VIEW_PROPERTY(policyKey, NSString);
20+
RCT_EXPORT_VIEW_PROPERTY(accountId, NSString);
21+
RCT_EXPORT_VIEW_PROPERTY(videoId, NSString);
22+
RCT_EXPORT_VIEW_PROPERTY(referenceId, NSString);
23+
RCT_EXPORT_VIEW_PROPERTY(videoToken, NSString);
24+
RCT_EXPORT_VIEW_PROPERTY(resizeMode, NSString);
25+
26+
@end

‎ios/BrightcovePlayerUtil.m

+48-4
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
#import "BrightcovePlayerUtil.h"
33
#import "BrightcovePlayerOfflineVideoManager.h"
44

5-
static BOOL sharedOfflineVideoManagerInitialized = false;
6-
75
@implementation BrightcovePlayerUtil
86

97
RCT_EXPORT_MODULE();
@@ -53,7 +51,7 @@ - (dispatch_queue_t)methodQueue {
5351
}
5452

5553
RCT_EXPORT_METHOD(getOfflineVideoStatuses:(NSString *)accountId policyKey:(NSString *)policyKey resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
56-
resolve([self collecteOfflineVideoStatuses]);
54+
resolve([self collectOfflineVideoStatuses]);
5755
}
5856

5957
RCT_EXPORT_METHOD(deleteOfflineVideo:(NSString *)accountId policyKey:(NSString *)policyKey videoToken:(NSString *)videoToken resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
@@ -62,7 +60,29 @@ - (dispatch_queue_t)methodQueue {
6260
resolve(nil);
6361
}
6462

65-
- (NSArray *)collecteOfflineVideoStatuses {
63+
RCT_EXPORT_METHOD(getPlaylistWithPlaylistId:(NSString *)playlistId accountId:(NSString *)accountId policyKey:(NSString *)policyKey resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
64+
BCOVPlaybackService* playbackService = [[BCOVPlaybackService alloc] initWithAccountId:accountId policyKey:policyKey];
65+
[playbackService findPlaylistWithPlaylistID:playlistId parameters:nil completion:^(BCOVPlaylist *playlist, NSDictionary *jsonResponse, NSError *error) {
66+
if (error) {
67+
reject(@"error", error.description, error);
68+
return;
69+
}
70+
resolve([self collectPlaylist:playlist]);
71+
}];
72+
}
73+
74+
RCT_EXPORT_METHOD(getPlaylistWithReferenceId:(NSString *)referenceId accountId:(NSString *)accountId policyKey:(NSString *)policyKey resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
75+
BCOVPlaybackService* playbackService = [[BCOVPlaybackService alloc] initWithAccountId:accountId policyKey:policyKey];
76+
[playbackService findPlaylistWithReferenceID:referenceId parameters:nil completion:^(BCOVPlaylist *playlist, NSDictionary *jsonResponse, NSError *error) {
77+
if (error) {
78+
reject(@"error", error.description, error);
79+
return;
80+
}
81+
resolve([self collectPlaylist:playlist]);
82+
}];
83+
}
84+
85+
- (NSArray *)collectOfflineVideoStatuses {
6686
NSArray* statuses = [[BrightcovePlayerOfflineVideoManager sharedManager] offlineVideoStatus];
6787
NSMutableArray *results = [[NSMutableArray alloc] init];
6888
for (BCOVOfflineVideoStatus *status in statuses) {
@@ -75,6 +95,30 @@ - (NSArray *)collecteOfflineVideoStatuses {
7595
return results;
7696
}
7797

98+
- (NSArray *)collectPlaylist:(BCOVPlaylist *)playlist {
99+
NSMutableArray *videos = [[NSMutableArray alloc] init];
100+
for (BCOVVideo *video in playlist.videos) {
101+
NSString *name = video.properties[kBCOVVideoPropertyKeyName];
102+
if (!name) {
103+
name = @"";
104+
}
105+
NSString *description = video.properties[kBCOVVideoPropertyKeyDescription];
106+
if (!description) {
107+
description = @"";
108+
}
109+
[videos addObject:
110+
@{
111+
@"accountId": video.properties[kBCOVVideoPropertyKeyAccountId],
112+
@"videoId": video.properties[kBCOVVideoPropertyKeyId],
113+
@"referenceId": video.properties[kBCOVVideoPropertyKeyReferenceId],
114+
@"name": name,
115+
@"description": description,
116+
@"duration": video.properties[kBCOVVideoPropertyKeyDuration]
117+
}];
118+
}
119+
return videos;
120+
}
121+
78122
- (NSDictionary *)generateDownloadParameterWithBitRate:(NSNumber*)bitRate {
79123
return
80124
@{

0 commit comments

Comments
 (0)
Please sign in to comment.