Skip to content

Commit

Permalink
Merge pull request #135 from zhgchgli0718/change-and-add-item-change-…
Browse files Browse the repository at this point in the history
…delegate

Optimize hysteriaPlayerCurrentItemChanged
  • Loading branch information
saiday authored Jan 21, 2021
2 parents b032bad + 64981d2 commit 8ce5063
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions HysteriaPlayer/HysteriaPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ typedef NS_ENUM(NSInteger, HysteriaPlayerFailed) {
@optional
- (void)hysteriaPlayerWillChangedAtIndex:(NSInteger)index;
- (void)hysteriaPlayerCurrentItemChanged:(AVPlayerItem *)item;
- (void)hysteriaPlayerCurrentItemEvicted:(AVPlayerItem *)item;
- (void)hysteriaPlayerRateChanged:(BOOL)isPlaying;
- (void)hysteriaPlayerDidReachEnd;
- (void)hysteriaPlayerCurrentItemPreloaded:(CMTime)time;
Expand Down
6 changes: 5 additions & 1 deletion HysteriaPlayer/HysteriaPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -765,13 +765,17 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
if (object == self.audioPlayer && [keyPath isEqualToString:@"currentItem"]) {
AVPlayerItem *newPlayerItem = [change objectForKey:NSKeyValueChangeNewKey];
AVPlayerItem *lastPlayerItem = [change objectForKey:NSKeyValueChangeOldKey];
if (lastPlayerItem != (id)[NSNull null]) {
if (lastPlayerItem != (id)[NSNull null] && lastPlayerItem != nil) {
@try {
[lastPlayerItem removeObserver:self forKeyPath:@"loadedTimeRanges" context:nil];
[lastPlayerItem removeObserver:self forKeyPath:@"status" context:nil];
} @catch(id anException) {
//do nothing, obviously it wasn't attached because an exception was thrown
}

if ([self.delegate respondsToSelector:@selector(hysteriaPlayerCurrentItemEvicted:)]) {
[self.delegate hysteriaPlayerCurrentItemEvicted:lastPlayerItem];
}
}
if (newPlayerItem != (id)[NSNull null]) {
[newPlayerItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
Expand Down

0 comments on commit 8ce5063

Please sign in to comment.