11
22
33#import " ASVideoNode.h"
4- #import " ASDisplayNodeInternal.h"
5- #import " ASDisplayNode+Subclasses.h"
6- #import " ASDisplayNode+FrameworkPrivate.h"
7-
8- @interface ASDisplayNode ()
9- - (void )setInterfaceState : (ASInterfaceState)newState ;
10- @end
114
125@interface ASVideoNode () {
136 ASDN::RecursiveMutex _lock;
@@ -24,13 +17,16 @@ @interface ASVideoNode () {
2417
2518@end
2619
20+ @interface ASDisplayNode ()
21+ - (void )setInterfaceState : (ASInterfaceState)newState ;
22+ @end
23+
2724@implementation ASVideoNode
2825
2926- (instancetype )init {
3027 if (!(self = [super init ])) { return nil ; }
3128
3229 _playerNode = [[ASDisplayNode alloc ] initWithLayerBlock: ^CALayer *{ return [[AVPlayerLayer alloc ] init ]; }];
33-
3430 [self addSubnode: _playerNode];
3531
3632 self.gravity = ASVideoGravityResizeAspect;
@@ -114,7 +110,7 @@ - (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock
114110- (void )fetchData
115111{
116112 [super fetchData ];
117-
113+
118114 @try {
119115 [_currentItem removeObserver: self forKeyPath: NSStringFromSelector (@selector (status ))];
120116 }
@@ -124,18 +120,46 @@ - (void)fetchData
124120
125121 {
126122 ASDN::MutexLocker l (_lock);
127-
128123 _currentItem = [[AVPlayerItem alloc ] initWithAsset: _asset];
129124 [_currentItem addObserver: self forKeyPath: NSStringFromSelector (@selector (status )) options: NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context: NULL ];
130125
131- [((AVPlayerLayer *)_playerNode.layer).player replaceCurrentItemWithPlayerItem: _currentItem];
126+ if (((AVPlayerLayer *)_playerNode.layer ).player ) {
127+ [((AVPlayerLayer *)_playerNode.layer).player replaceCurrentItemWithPlayerItem: _currentItem];
128+ } else {
129+ ((AVPlayerLayer *)_playerNode.layer ).player = [[AVPlayer alloc ] initWithPlayerItem: _currentItem];
130+ }
131+
132132 }
133-
134133 if (_shouldAutoPlay) {
135134 [self play ];
136135 }
137136}
138137
138+ // - (void)fetchData
139+ // {
140+ // [super fetchData];
141+ //
142+ // @try {
143+ // [_currentItem removeObserver:self forKeyPath:NSStringFromSelector(@selector(status))];
144+ // }
145+ // @catch (NSException * __unused exception) {
146+ // NSLog(@"unnecessary removal in fetch data");
147+ // }
148+ //
149+ // {
150+ // ASDN::MutexLocker l(_lock);
151+ //
152+ // _currentItem = [[AVPlayerItem alloc] initWithAsset:_asset];
153+ // [_currentItem addObserver:self forKeyPath:NSStringFromSelector(@selector(status)) options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:NULL];
154+ //
155+ // [((AVPlayerLayer *)_playerNode.layer).player replaceCurrentItemWithPlayerItem:_currentItem];
156+ // }
157+ //
158+ // if (_shouldAutoPlay) {
159+ // [self play];
160+ // }
161+ // }
162+
139163- (void )clearFetchedData
140164{
141165 [super clearFetchedData ];
@@ -150,7 +174,7 @@ - (void)clearFetchedData
150174- (void )setPlayButton : (ASButtonNode *)playButton
151175{
152176 ASDN::MutexLocker l (_lock);
153-
177+
154178 _playButton = playButton;
155179
156180 [self addSubnode: playButton];
@@ -161,7 +185,7 @@ - (void)setPlayButton:(ASButtonNode *)playButton
161185- (ASButtonNode *)playButton
162186{
163187 ASDN::MutexLocker l (_lock);
164-
188+
165189 return _playButton;
166190}
167191
@@ -188,7 +212,7 @@ - (AVAsset *)asset
188212
189213- (void )setGravity : (ASVideoGravity)gravity {
190214 ASDN::MutexLocker l (_lock);
191-
215+
192216 switch (gravity) {
193217 case ASVideoGravityResize:
194218 ((AVPlayerLayer *)_playerNode.layer ).videoGravity = AVLayerVideoGravityResize;
@@ -208,7 +232,7 @@ - (void)setGravity:(ASVideoGravity)gravity {
208232- (ASVideoGravity)gravity ;
209233{
210234 ASDN::MutexLocker l (_lock);
211-
235+
212236 if ([((AVPlayerLayer *)_playerNode.layer).contentsGravity isEqualToString: AVLayerVideoGravityResize]) {
213237 return ASVideoGravityResize;
214238 }
@@ -222,7 +246,7 @@ - (ASVideoGravity)gravity;
222246- (void )play ;
223247{
224248 ASDN::MutexLocker l (_lock);
225-
249+
226250 [[((AVPlayerLayer *)_playerNode.layer) player ] play ];
227251 _shouldBePlaying = YES ;
228252 _playButton.alpha = 0.0 ;
@@ -231,7 +255,7 @@ - (void)play;
231255 UIActivityIndicatorView *spinnnerView = [[UIActivityIndicatorView alloc ] initWithFrame: _playButton.frame];
232256 spinnnerView.color = [UIColor whiteColor ];
233257 [spinnnerView startAnimating ];
234-
258+
235259 return spinnnerView;
236260 }];
237261
@@ -246,8 +270,8 @@ - (BOOL)ready;
246270
247271- (void )pause ;
248272{
249- // ASDN::MutexLocker l(_lock);
250-
273+ ASDN::MutexLocker l (_lock);
274+
251275 [[((AVPlayerLayer *)_playerNode.layer) player ] pause ];
252276 _shouldBePlaying = NO ;
253277 _playButton.alpha = 1.0 ;
@@ -265,3 +289,4 @@ - (void)dealloc
265289}
266290
267291@end
292+
0 commit comments