Repository: incubator-weex Updated Branches: refs/heads/master 5b901c007 -> 939bc5454
+ [iOS] add WXVideoComponent "poster" attribute. Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/946bcec8 Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/946bcec8 Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/946bcec8 Branch: refs/heads/master Commit: 946bcec8fbbfbb4d3bd1767e3a8d55a759576017 Parents: a59ddfe Author: å¾æé³ <[email protected]> Authored: Wed Mar 7 10:59:44 2018 +0800 Committer: å¾æé³ <[email protected]> Committed: Wed Mar 7 10:59:44 2018 +0800 ---------------------------------------------------------------------- .../Sources/Component/WXVideoComponent.h | 2 + .../Sources/Component/WXVideoComponent.m | 65 ++++++++++++++++++++ 2 files changed, 67 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/946bcec8/ios/sdk/WeexSDK/Sources/Component/WXVideoComponent.h ---------------------------------------------------------------------- diff --git a/ios/sdk/WeexSDK/Sources/Component/WXVideoComponent.h b/ios/sdk/WeexSDK/Sources/Component/WXVideoComponent.h index 4703a7a..bf9d908 100644 --- a/ios/sdk/WeexSDK/Sources/Component/WXVideoComponent.h +++ b/ios/sdk/WeexSDK/Sources/Component/WXVideoComponent.h @@ -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; http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/946bcec8/ios/sdk/WeexSDK/Sources/Component/WXVideoComponent.m ---------------------------------------------------------------------- diff --git a/ios/sdk/WeexSDK/Sources/Component/WXVideoComponent.m b/ios/sdk/WeexSDK/Sources/Component/WXVideoComponent.m index d9aadc4..42c61e2 100644 --- a/ios/sdk/WeexSDK/Sources/Component/WXVideoComponent.m +++ b/ios/sdk/WeexSDK/Sources/Component/WXVideoComponent.m @@ -21,6 +21,7 @@ #import "WXHandlerFactory.h" #import "WXURLRewriteProtocol.h" #import "WXSDKEngine.h" +#import "WXImgLoaderProtocol.h" #import <AVFoundation/AVPlayer.h> #import <AVKit/AVPlayerViewController.h> @@ -41,6 +42,9 @@ @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 @@ -85,6 +89,13 @@ } [self addSubview:_playerViewController.view]; + + _posterImageView = [[UIImageView alloc] init]; + _posterImageView.userInteractionEnabled = YES; + [_posterImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(posterTapHandler)]]; + _posterImageView.hidden = YES; + [self addSubview:_posterImageView]; + [self bringSubviewToFront:_posterImageView]; } return self; } @@ -147,6 +158,7 @@ videoFrame.origin.x = 0; videoFrame.origin.y = 0; [_playerViewController.view setFrame:videoFrame]; + [_posterImageView setFrame:videoFrame]; } - (void)setURL:(NSURL *)URL @@ -188,6 +200,24 @@ } } +- (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) @@ -203,6 +233,7 @@ - (void)play { + _posterImageView.hidden = YES; if ([self greater8SysVer]) { AVPlayerViewController *AVVC = (AVPlayerViewController*)_playerViewController; @@ -224,12 +255,35 @@ } } +- (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; @@ -252,6 +306,9 @@ if ([attributes[@"playStatus"] compare:@"pause" options:NSCaseInsensitiveSearch] == NSOrderedSame) { _playStatus = false; } + if (attributes[@"poster"]) { + _posterURL = [NSURL URLWithString: attributes[@"poster"]]; + } } return self; } @@ -268,6 +325,7 @@ { _videoView = (WXVideoView *)self.view; [_videoView setURL:_videoURL]; + [_videoView setPosterURL:_posterURL]; if (_playStatus) { [_videoView play]; } else { @@ -277,6 +335,9 @@ [_videoView play]; } __weak __typeof__(self) weakSelf = self; + _videoView.posterClickHandle = ^{ + [weakSelf.videoView play]; + }; _videoView.playbackStateChanged = ^(WXPlaybackState state) { NSString *eventType = nil; switch (state) { @@ -319,6 +380,10 @@ _playStatus = false; [_videoView pause]; } + if (attributes[@"poster"]) { + _posterURL = [NSURL URLWithString: attributes[@"poster"]]; + [_videoView setPosterURL:_posterURL]; + } } @end
