Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

[WEEX-241][iOS] add WXVideoComponent "poster" attribute #1051

Merged
merged 2 commits into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions ios/sdk/WeexSDK/Sources/Component/WXVideoComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ typedef NS_ENUM(NSInteger, WXPlaybackState) {
@interface WXVideoView : UIView

@property (nonatomic, copy) void (^playbackStateChanged)(WXPlaybackState state);
@property (nonatomic, copy) void (^posterClickHandle)(void);

- (void) setURL:(NSURL*)URL;
- (void) setPosterURL:(NSURL *)posterURL;

- (void) play;
- (void) pause;
Expand Down
69 changes: 69 additions & 0 deletions ios/sdk/WeexSDK/Sources/Component/WXVideoComponent.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#import "WXHandlerFactory.h"
#import "WXURLRewriteProtocol.h"
#import "WXSDKEngine.h"
#import "WXImgLoaderProtocol.h"

#import <AVFoundation/AVPlayer.h>
#import <AVKit/AVPlayerViewController.h>
Expand All @@ -41,6 +42,9 @@ @interface WXVideoView()
@property (nonatomic, strong) UIViewController* playerViewController;
@property (nonatomic, strong) AVPlayerItem* playerItem;
@property (nonatomic, strong) WXSDKInstance* weexSDKInstance;
@property (nonatomic, strong) UIImageView *posterImageView;
@property (nonatomic, strong) id<WXImageOperationProtocol> imageOperation;
@property (nonatomic, assign) BOOL playerDidPlayed;

@end

Expand Down Expand Up @@ -188,6 +192,24 @@ - (void)setURL:(NSURL *)URL
}
}

- (void)setPosterURL:(NSURL *)posterURL {
if (!posterURL) {
return;
}

[self cancelImage];
__weak typeof(self) weakSelf = self;
weakSelf.imageOperation = [[self imageLoader] downloadImageWithURL:posterURL.absoluteString imageFrame:self.posterImageView.frame userInfo:nil completed:^(UIImage *image, NSError *error, BOOL finished) {
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(self) strongSelf = weakSelf;
if (!error) {
strongSelf.posterImageView.image = image;
strongSelf.posterImageView.hidden = strongSelf.playerDidPlayed;
}
});
}];
}

- (void)playFinish
{
if (_playbackStateChanged)
Expand All @@ -203,6 +225,7 @@ - (void)playFinish

- (void)play
{
_posterImageView.hidden = YES;
if ([self greater8SysVer]) {
AVPlayerViewController *AVVC = (AVPlayerViewController*)_playerViewController;

Expand All @@ -224,12 +247,47 @@ - (void)pause
}
}

- (void)posterTapHandler {
if (self.posterClickHandle) {
self.posterClickHandle();
}
}

- (UIImageView *)posterImageView {
if (!_posterImageView) {
_posterImageView = [[UIImageView alloc] initWithFrame:self.bounds];
_posterImageView.userInteractionEnabled = YES;
[_posterImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(posterTapHandler)]];
_posterImageView.hidden = YES;
[self addSubview:_posterImageView];
[self bringSubviewToFront:_posterImageView];
}
return _posterImageView;
}

- (id<WXImgLoaderProtocol>)imageLoader
{
static id<WXImgLoaderProtocol> imageLoader;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
imageLoader = [WXHandlerFactory handlerForProtocol:@protocol(WXImgLoaderProtocol)];
});
return imageLoader;
}

- (void)cancelImage
{
[_imageOperation cancel];
_imageOperation = nil;
}

@end

@interface WXVideoComponent()

@property (nonatomic, weak) WXVideoView *videoView;
@property (nonatomic, strong) NSURL *videoURL;
@property (nonatomic, strong) NSURL *posterURL;
@property (nonatomic) BOOL autoPlay;
@property (nonatomic) BOOL playStatus;

Expand All @@ -252,6 +310,9 @@ - (instancetype)initWithRef:(NSString *)ref type:(NSString *)type styles:(NSDict
if ([attributes[@"playStatus"] compare:@"pause" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
_playStatus = false;
}
if (attributes[@"poster"]) {
_posterURL = [NSURL URLWithString: attributes[@"poster"]];
}
}
return self;
}
Expand All @@ -268,6 +329,7 @@ -(void)viewDidLoad
{
_videoView = (WXVideoView *)self.view;
[_videoView setURL:_videoURL];
[_videoView setPosterURL:_posterURL];
if (_playStatus) {
[_videoView play];
} else {
Expand All @@ -277,6 +339,9 @@ -(void)viewDidLoad
[_videoView play];
}
__weak __typeof__(self) weakSelf = self;
_videoView.posterClickHandle = ^{
[weakSelf.videoView play];
};
_videoView.playbackStateChanged = ^(WXPlaybackState state) {
NSString *eventType = nil;
switch (state) {
Expand Down Expand Up @@ -319,6 +384,10 @@ -(void)updateAttributes:(NSDictionary *)attributes
_playStatus = false;
[_videoView pause];
}
if (attributes[@"poster"]) {
_posterURL = [NSURL URLWithString: attributes[@"poster"]];
[_videoView setPosterURL:_posterURL];
}
}

@end