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 1 commit
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
65 changes: 65 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 @@ -85,6 +89,13 @@ - (id)init
}

[self addSubview:_playerViewController.view];

_posterImageView = [[UIImageView alloc] init];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about lazy load when front-end developer specify the poster attributes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good ideal! I will modify this later.

_posterImageView.userInteractionEnabled = YES;
[_posterImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(posterTapHandler)]];
_posterImageView.hidden = YES;
[self addSubview:_posterImageView];
[self bringSubviewToFront:_posterImageView];
}
return self;
}
Expand Down Expand Up @@ -147,6 +158,7 @@ - (void)setFrame:(CGRect)frame
videoFrame.origin.x = 0;
videoFrame.origin.y = 0;
[_playerViewController.view setFrame:videoFrame];
[_posterImageView setFrame:videoFrame];
}

- (void)setURL:(NSURL *)URL
Expand Down Expand Up @@ -188,6 +200,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 +233,7 @@ - (void)playFinish

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

Expand All @@ -224,12 +255,35 @@ - (void)pause
}
}

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

- (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 +306,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 +325,7 @@ -(void)viewDidLoad
{
_videoView = (WXVideoView *)self.view;
[_videoView setURL:_videoURL];
[_videoView setPosterURL:_posterURL];
if (_playStatus) {
[_videoView play];
} else {
Expand All @@ -277,6 +335,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 +380,10 @@ -(void)updateAttributes:(NSDictionary *)attributes
_playStatus = false;
[_videoView pause];
}
if (attributes[@"poster"]) {
_posterURL = [NSURL URLWithString: attributes[@"poster"]];
[_videoView setPosterURL:_posterURL];
}
}

@end