Add cache property; Make playerItemForSource in 'RCTVideoManager.m' async

This commit is contained in:
Laurin Quast 2018-02-28 17:42:49 +01:00
parent 6b2c1046dd
commit 438aa79494
3 changed files with 283 additions and 266 deletions

View File

@ -247,6 +247,7 @@ export default class Video extends Component {
Video.propTypes = {
/* Native only */
src: PropTypes.object,
cache: PropTypes.bool,
seek: PropTypes.number,
fullscreen: PropTypes.bool,
onVideoLoadStart: PropTypes.func,

View File

@ -266,11 +266,23 @@ static NSString *const timedMetadata = @"timedMetadata";
#pragma mark - Player and source
- (void)setCache:(BOOL *)cache
{
// @TODO: Implement
}
- (void)setSrc:(NSDictionary *)source
{
[self removePlayerTimeObserver];
[self removePlayerItemObservers];
_playerItem = [self playerItemForSource:source];
[self playerItemForSource:source withCallback:^(AVPlayerItem * playerItem) {
[self didSetPlayerItemWithSource:source playerItem:playerItem];
}];
}
- (void) didSetPlayerItemWithSource:(NSDictionary *)source playerItem:(AVPlayerItem *) playerItem
{
_playerItem = playerItem;
[self addPlayerItemObservers];
[_player pause];
@ -312,7 +324,7 @@ static NSString *const timedMetadata = @"timedMetadata";
});
}
- (AVPlayerItem*)playerItemForSource:(NSDictionary *)source
- (void)playerItemForSource:(NSDictionary *)source withCallback:(void(^)(AVPlayerItem *))handler
{
bool isNetwork = [RCTConvert BOOL:[source objectForKey:@"isNetwork"]];
bool isAsset = [RCTConvert BOOL:[source objectForKey:@"isAsset"]];
@ -324,16 +336,19 @@ static NSString *const timedMetadata = @"timedMetadata";
[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:uri ofType:type]];
if (isNetwork) {
// @TODO: Check if item is cached an if so use the cached asset
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:@{AVURLAssetHTTPCookiesKey : cookies}];
return [AVPlayerItem playerItemWithAsset:asset];
handler([AVPlayerItem playerItemWithAsset:asset]);
return;
}
else if (isAsset) {
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
return [AVPlayerItem playerItemWithAsset:asset];
handler([AVPlayerItem playerItemWithAsset:asset]);
return;
}
return [AVPlayerItem playerItemWithURL:url];
handler([AVPlayerItem playerItemWithURL:url]);
return;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

View File

@ -20,6 +20,7 @@ RCT_EXPORT_MODULE();
}
RCT_EXPORT_VIEW_PROPERTY(src, NSDictionary);
RCT_EXPORT_VIEW_PROPERTY(cache, BOOL);
RCT_EXPORT_VIEW_PROPERTY(resizeMode, NSString);
RCT_EXPORT_VIEW_PROPERTY(repeat, BOOL);
RCT_EXPORT_VIEW_PROPERTY(paused, BOOL);