Skip to content

Commit dd98c6d

Browse files
authored
Merge pull request #63 from CloudlessMoon/main
新增FSPlayerCurrentPlaybackTimeDidChangeNotification,业务可以监听此通知用来更新播放进度,减少业务的代码复杂度
2 parents 4e906bf + 7e2cdcf commit dd98c6d

File tree

7 files changed

+83
-14
lines changed

7 files changed

+83
-14
lines changed

examples/ios/FSPlayerDemo/ViewController/FSMediaControl.m

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,6 @@ - (void)refreshMediaControl
113113
BOOL isPlaying = [self.delegatePlayer isPlaying];
114114
self.playButton.hidden = isPlaying;
115115
self.pauseButton.hidden = !isPlaying;
116-
117-
118-
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(refreshMediaControl) object:nil];
119-
if (!self.overlayPanel.hidden) {
120-
[self performSelector:@selector(refreshMediaControl) withObject:nil afterDelay:0.5];
121-
}
122116
}
123117

124118
#pragma mark IBAction

examples/ios/FSPlayerDemo/ViewController/FSMoviePlayerViewController.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ - (void)viewDidLoad
113113
}
114114
options.metalRenderer = YES;
115115
options.automaticallySetupAudioSession = YES;
116+
options.currentPlaybackTimeNotificationInterval = 0.5;
116117

117118
self.player = [[FSPlayer alloc] initWithContentURL:self.url withOptions:options];
118119
self.player.playbackLoop = 2;
@@ -376,6 +377,8 @@ - (void)moviePlayBackDidFinish:(NSNotification*)notification
376377
NSLog(@"playbackPlayBackDidFinish: ???: %d\n", reason);
377378
break;
378379
}
380+
381+
[self.mediaControl refreshMediaControl];
379382
}
380383

381384
- (void)mediaIsPreparedToPlayDidChange:(NSNotification*)notification
@@ -420,12 +423,19 @@ - (void)moviePlayBackStateDidChange:(NSNotification*)notification
420423
break;
421424
}
422425
}
426+
427+
[self.mediaControl refreshMediaControl];
423428
}
424429

425430
- (void)moviePlayBackBufferingDidChange:(NSNotification*)notification {
426431
NSLog(@"moviePlayBackBufferingDidChange: %f, currentTime: %f", _player.playableDuration, _player.currentPlaybackTime);
427432
}
428433

434+
- (void)moviePlayBackCurrentPlaybackTimeDidChange:(NSNotification*)notification {
435+
NSLog(@"moviePlayBackCurrentPlaybackTimeDidChange: %f", _player.currentPlaybackTime);
436+
[self.mediaControl refreshMediaControl];
437+
}
438+
429439
#pragma mark Install Movie Notifications
430440

431441
/* Register observers for the various movie object notifications. */
@@ -455,6 +465,11 @@ -(void)installMovieNotificationObservers
455465
selector:@selector(moviePlayBackBufferingDidChange:)
456466
name:FSPlayerBufferingDidChangeNotification
457467
object:_player];
468+
469+
[[NSNotificationCenter defaultCenter] addObserver:self
470+
selector:@selector(moviePlayBackCurrentPlaybackTimeDidChange:)
471+
name:FSPlayerCurrentPlaybackTimeDidChangeNotification
472+
object:_player];
458473
}
459474

460475
#pragma mark Remove Movie Notification Handlers
@@ -467,6 +482,7 @@ -(void)removeMovieNotificationObservers
467482
[[NSNotificationCenter defaultCenter]removeObserver:self name:FSPlayerIsPreparedToPlayNotification object:_player];
468483
[[NSNotificationCenter defaultCenter]removeObserver:self name:FSPlayerPlaybackStateDidChangeNotification object:_player];
469484
[[NSNotificationCenter defaultCenter]removeObserver:self name:FSPlayerBufferingDidChangeNotification object:_player];
485+
[[NSNotificationCenter defaultCenter]removeObserver:self name:FSPlayerCurrentPlaybackTimeDidChangeNotification object:_player];
470486
}
471487

472488
@end

ijkmedia/wrapper/apple/FSMediaPlayback.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ FS_EXTERN NSString* const FSPlayerIsAirPlayVideoActiveDidChangeNotification;
189189
// Posted when the buffering changes.
190190
FS_EXTERN NSString* const FSPlayerBufferingDidChangeNotification;
191191

192+
// Posted when the current playback time changes.
193+
FS_EXTERN NSString* const FSPlayerCurrentPlaybackTimeDidChangeNotification;
194+
192195
// -----------------------------------------------------------------------------
193196
// Movie Property Notifications
194197

ijkmedia/wrapper/apple/FSMediaPlayback.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
NSString *const FSPlayerBufferingDidChangeNotification = @"FSPlayerBufferingDidChangeNotification";
3838

39+
NSString *const FSPlayerCurrentPlaybackTimeDidChangeNotification = @"FSPlayerCurrentPlaybackTimeDidChangeNotification";
40+
3941
NSString *const FSPlayerNaturalSizeAvailableNotification = @"FSPlayerNaturalSizeAvailableNotification";
4042
NSString *const FSPlayerZRotateAvailableNotification = @"FSPlayerZRotateAvailableNotification";
4143
NSString *const FSPlayerNoCodecFoundNotification = @"FSPlayerNoCodecFoundNotification";

ijkmedia/wrapper/apple/FSOptions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ NS_ASSUME_NONNULL_BEGIN
6868
@property(nonatomic, copy) NSString *protocolWhitelist;
6969
//setup audio session use AVAudioSessionCategoryPlayback ,observer and handle interrupt event
7070
@property(nonatomic) BOOL automaticallySetupAudioSession;
71+
//default is 0, when the value is 0, it is not turned on.
72+
@property(nonatomic) NSTimeInterval currentPlaybackTimeNotificationInterval;
7173

7274
@end
7375
NS_ASSUME_NONNULL_END

ijkmedia/wrapper/apple/FSOptions.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ + (FSOptions *)optionsByDefault
5151
options.showHudView = NO;
5252
options.metalRenderer = YES;
5353
options.automaticallySetupAudioSession = YES;
54+
options.currentPlaybackTimeNotificationInterval = 0;
5455
return options;
5556
}
5657

ijkmedia/wrapper/apple/FSPlayer.m

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ @implementation FSPlayer {
8787
#endif
8888
int _enableAccurateSeek;
8989
BOOL _canUpdateAccurateSeek;
90+
91+
NSTimer *_currentPlaybackTimeTimer;
92+
NSTimeInterval _currentPlaybackTimeNotificationInterval;
9093
}
9194

9295
@synthesize view = _view;
@@ -124,6 +127,7 @@ static void FSPlayerSafeDestroy(FSPlayer *player) {
124127
__block FSSDLHudControl *hudCtrl = player->_hudCtrl;
125128
__block NSTimer *hudTimer = player->_hudTimer;
126129
__block UIView<FSVideoRenderingProtocol> *view = player->_view;
130+
__block NSTimer *currentPlaybackTimeTimer = player->_currentPlaybackTimeTimer;
127131

128132
#if TARGET_OS_IOS
129133
[player unregisterApplicationObservers];
@@ -134,6 +138,7 @@ static void FSPlayerSafeDestroy(FSPlayer *player) {
134138
player->_view = nil;
135139
player->_hudCtrl = nil;
136140
player->_hudTimer = nil;
141+
player->_currentPlaybackTimeTimer = nil;
137142

138143
player->_segmentOpenDelegate = nil;
139144
player->_tcpOpenDelegate = nil;
@@ -158,6 +163,9 @@ static void FSPlayerSafeDestroy(FSPlayer *player) {
158163
hudTimer = nil;
159164
[hudCtrl destroyContentView];
160165
hudCtrl = nil;
166+
/// currentPlaybackTime
167+
[currentPlaybackTimeTimer invalidate];
168+
currentPlaybackTimeTimer = nil;
161169
};
162170
if ([NSThread isMainThread]) {
163171
UIHandler();
@@ -206,6 +214,7 @@ - (void)_initWithContent:(NSURL *)aUrl
206214
_scalingMode = FSScalingModeAspectFit;
207215
_shouldAutoplay = YES;
208216
_canUpdateAccurateSeek = YES;
217+
_currentPlaybackTimeNotificationInterval = options.currentPlaybackTimeNotificationInterval;
209218

210219
memset(&_asyncStat, 0, sizeof(_asyncStat));
211220
memset(&_cacheStat, 0, sizeof(_cacheStat));
@@ -1097,11 +1106,10 @@ - (void)startHudTimer
10971106
hudView.frame = rect;
10981107
[self.view addSubview:hudView];
10991108

1100-
_hudTimer = [NSTimer scheduledTimerWithTimeInterval:.5f
1101-
target:self
1102-
selector:@selector(refreshHudView)
1103-
userInfo:nil
1104-
repeats:YES];
1109+
__weak __typeof(self) weakSelf = self;
1110+
_hudTimer = [NSTimer scheduledTimerWithTimeInterval:.5f repeats:YES block:^(NSTimer *timer) {
1111+
[weakSelf refreshHudView];
1112+
}];
11051113
} else {
11061114
dispatch_async(dispatch_get_main_queue(), ^{
11071115
[self startHudTimer];
@@ -1451,9 +1459,13 @@ - (void)postEvent: (FSPlayerMessage *)msg
14511459
[self setScreenOn:NO];
14521460

14531461
[[NSNotificationCenter defaultCenter]
1454-
postNotificationName:FSPlayerPlaybackStateDidChangeNotification
1462+
postNotificationName:FSPlayerCurrentPlaybackTimeDidChangeNotification
14551463
object:self];
14561464

1465+
[[NSNotificationCenter defaultCenter]
1466+
postNotificationName:FSPlayerPlaybackStateDidChangeNotification
1467+
object:self];
1468+
14571469
[[NSNotificationCenter defaultCenter]
14581470
postNotificationName:FSPlayerDidFinishNotification
14591471
object:self
@@ -1486,25 +1498,37 @@ - (void)postEvent: (FSPlayerMessage *)msg
14861498
ijkmp_set_playback_volume(_mediaPlayer, [self playbackVolume]);
14871499

14881500
[self startHudTimer];
1501+
[self createCurrentPlaybackTimeTimer];
14891502
_isPreparedToPlay = YES;
14901503

14911504
[[NSNotificationCenter defaultCenter] postNotificationName:FSPlayerIsPreparedToPlayNotification object:self];
14921505
_loadState = FSPlayerLoadStatePlayable | FSPlayerLoadStatePlaythroughOK;
14931506

1507+
[[NSNotificationCenter defaultCenter]
1508+
postNotificationName:FSPlayerCurrentPlaybackTimeDidChangeNotification
1509+
object:self];
1510+
1511+
[[NSNotificationCenter defaultCenter]
1512+
postNotificationName:FSPlayerBufferingDidChangeNotification
1513+
object:self];
1514+
14941515
[[NSNotificationCenter defaultCenter]
14951516
postNotificationName:FSPlayerLoadStateDidChangeNotification
14961517
object:self];
1497-
14981518
break;
14991519
}
15001520
case FFP_MSG_COMPLETED: {
15011521

15021522
[self setScreenOn:NO];
15031523

1524+
[[NSNotificationCenter defaultCenter]
1525+
postNotificationName:FSPlayerCurrentPlaybackTimeDidChangeNotification
1526+
object:self];
1527+
15041528
[[NSNotificationCenter defaultCenter]
15051529
postNotificationName:FSPlayerPlaybackStateDidChangeNotification
15061530
object:self];
1507-
1531+
15081532
[[NSNotificationCenter defaultCenter]
15091533
postNotificationName:FSPlayerDidFinishNotification
15101534
object:self
@@ -1578,6 +1602,11 @@ - (void)postEvent: (FSPlayerMessage *)msg
15781602
object:self
15791603
userInfo:@{FSPlayerDidSeekCompleteTargetKey: @(avmsg->arg1),
15801604
FSPlayerDidSeekCompleteErrorKey: @(avmsg->arg2)}];
1605+
1606+
[[NSNotificationCenter defaultCenter]
1607+
postNotificationName:FSPlayerCurrentPlaybackTimeDidChangeNotification
1608+
object:self];
1609+
15811610
_seeking = NO;
15821611
break;
15831612
}
@@ -1647,6 +1676,10 @@ - (void)postEvent: (FSPlayerMessage *)msg
16471676
postNotificationName:FSPlayerAccurateSeekCompleteNotification
16481677
object:self
16491678
userInfo:@{FSPlayerDidAccurateSeekCompleteCurPos: @(avmsg->arg1)}];
1679+
1680+
[[NSNotificationCenter defaultCenter]
1681+
postNotificationName:FSPlayerCurrentPlaybackTimeDidChangeNotification
1682+
object:self];
16501683
break;
16511684
}
16521685
case FFP_MSG_VIDEO_SEEK_RENDERING_START: {
@@ -1766,6 +1799,24 @@ - (void)setHudUrl:(NSURL *)url
17661799
}
17671800
}
17681801

1802+
- (void)createCurrentPlaybackTimeTimer {
1803+
if (!_currentPlaybackTimeTimer && _currentPlaybackTimeNotificationInterval > 0) {
1804+
__weak __typeof(self) weakSelf = self;
1805+
_currentPlaybackTimeTimer = [NSTimer scheduledTimerWithTimeInterval:_currentPlaybackTimeNotificationInterval repeats:YES block:^(NSTimer *timer) {
1806+
[weakSelf callCurrentPlaybackTimeNotification];
1807+
}];
1808+
}
1809+
}
1810+
1811+
- (void)callCurrentPlaybackTimeNotification {
1812+
if (!_mediaPlayer || !self.isPlaying) {
1813+
return;
1814+
}
1815+
[[NSNotificationCenter defaultCenter]
1816+
postNotificationName:FSPlayerCurrentPlaybackTimeDidChangeNotification
1817+
object:self];
1818+
}
1819+
17691820
#pragma mark av_format_control_message
17701821

17711822
static int onInjectIOControl(FSPlayer *mpc, id<FSMediaUrlOpenDelegate> delegate, int type, void *data, size_t data_size)

0 commit comments

Comments
 (0)