@@ -19,10 +19,13 @@ @interface ASVideoNode ()
1919 AVPlayerItem *_currentItem;
2020 AVPlayer *_player;
2121
22+ ASImageNode *_placeholderImageNode;
23+
2224 ASButtonNode *_playButton;
2325 ASDisplayNode *_playerNode;
2426 ASDisplayNode *_spinner;
2527 NSString *_gravity;
28+ dispatch_queue_t _previewQueue;
2629}
2730
2831@end
@@ -33,6 +36,8 @@ - (instancetype)init
3336{
3437 if (!(self = [super init ])) { return nil ; }
3538
39+ _previewQueue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_HIGH, 0 );
40+
3641 [ASDisplayNode setShouldUseNewRenderingRange: YES ];
3742
3843 self.gravity = AVLayerVideoGravityResizeAspect;
@@ -49,8 +54,6 @@ - (void)interfaceStateDidChange:(ASInterfaceState)newState fromState:(ASInterfac
4954 if (_shouldBePlaying) {
5055 [self pause ];
5156 _shouldBePlaying = YES ;
52- } else {
53- [self pause ];
5457 }
5558 [(UIActivityIndicatorView *)_spinner.view stopAnimating ];
5659 [_spinner removeFromSupernode ];
@@ -97,6 +100,8 @@ - (void)layout
97100 [super layout ];
98101
99102 CGRect bounds = self.bounds ;
103+
104+ _placeholderImageNode.frame = bounds;
100105 _playerNode.frame = bounds;
101106 _playerNode.layer .frame = bounds;
102107
@@ -112,18 +117,37 @@ - (void)didLoad
112117{
113118 [super didLoad ];
114119 [[NSNotificationCenter defaultCenter ] addObserver: self selector: @selector (didPlayToEnd: ) name: AVPlayerItemDidPlayToEndTimeNotification object: nil ];
115-
116- _playerNode = [[ASDisplayNode alloc ] initWithLayerBlock: ^CALayer *{
117- AVPlayerLayer *playerLayer = [[AVPlayerLayer alloc ] init ];
118- if (!_player) {
119- _player = [AVPlayer playerWithPlayerItem: [[AVPlayerItem alloc ] initWithAsset: _asset]];
120- }
121- playerLayer.player = _player;
122- playerLayer.videoGravity = [self gravity ];
123- return playerLayer;
124- }];
125-
126- [self insertSubnode: _playerNode atIndex: 0 ];
120+
121+ if (_shouldBePlaying) {
122+ _playerNode = [[ASDisplayNode alloc ] initWithLayerBlock: ^CALayer *{
123+ AVPlayerLayer *playerLayer = [[AVPlayerLayer alloc ] init ];
124+ if (!_player) {
125+ _player = [AVPlayer playerWithPlayerItem: [[AVPlayerItem alloc ] initWithAsset: _asset]];
126+ }
127+ playerLayer.player = _player;
128+ playerLayer.videoGravity = [self gravity ];
129+ return playerLayer;
130+ }];
131+
132+ [self insertSubnode: _playerNode atIndex: 0 ];
133+ } else {
134+ dispatch_async (_previewQueue, ^{
135+ AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc ] initWithAsset: _asset];
136+ [imageGenerator generateCGImagesAsynchronouslyForTimes: @[[NSValue valueWithCMTime: CMTimeMake (0 , 1 )]] completionHandler: ^(CMTime requestedTime, CGImageRef _Nullable image, CMTime actualTime, AVAssetImageGeneratorResult result, NSError * _Nullable error) {
137+ UIImage *theImage = [UIImage imageWithCGImage: image];
138+
139+ _placeholderImageNode = [[ASImageNode alloc ] init ];
140+ _placeholderImageNode.layerBacked = YES ;
141+ _placeholderImageNode.image = theImage;
142+ _placeholderImageNode.contentMode = UIViewContentModeScaleAspectFit;
143+
144+ dispatch_async (dispatch_get_main_queue (), ^{
145+ _placeholderImageNode.frame = self.bounds ;
146+ [self insertSubnode: _placeholderImageNode atIndex: 0 ];
147+ });
148+ }];
149+ });
150+ }
127151}
128152
129153- (void )tapped
@@ -277,6 +301,20 @@ - (void)play
277301 }];
278302 }
279303
304+ if (!_playerNode) {
305+ _playerNode = [[ASDisplayNode alloc ] initWithLayerBlock: ^CALayer *{
306+ AVPlayerLayer *playerLayer = [[AVPlayerLayer alloc ] init ];
307+ if (!_player) {
308+ _player = [AVPlayer playerWithPlayerItem: [[AVPlayerItem alloc ] initWithAsset: _asset]];
309+ }
310+ playerLayer.player = _player;
311+ playerLayer.videoGravity = [self gravity ];
312+ return playerLayer;
313+ }];
314+
315+ [self addSubnode: _playerNode];
316+ }
317+
280318 [_player play ];
281319 _shouldBePlaying = YES ;
282320 _playButton.alpha = 0.0 ;
@@ -355,4 +393,3 @@ - (void)dealloc
355393}
356394
357395@end
358-
0 commit comments