Quellcode durchsuchen

版本升级维护

yyqxiaoyin vor 5 Jahren
Ursprung
Commit
cafcce8dc8
1 geänderte Dateien mit 15 neuen und 1 gelöschten Zeilen
  1. 15 1
      MKRRadioManager/Classes/MKRRadioManager/MKRAVPlayer/MKRAVPlayer.m

+ 15 - 1
MKRRadioManager/Classes/MKRRadioManager/MKRAVPlayer/MKRAVPlayer.m

@@ -19,6 +19,7 @@ static void * PlayerStatusObservationContext = &PlayerStatusObservationContext;
 @interface MKRAVPlayer (){
     BOOL _interruptWhenPlaying;
 }
+@property (nonatomic, strong) id                timeObserver;
 @property (nonatomic, assign) UIBackgroundTaskIdentifier bgTaskId;//后台播放id
 @property (nonatomic, strong) AVPlayer *player;//播放器player
 @property (nonatomic, strong) AVPlayerItem *playerItem;//当前播放的item
@@ -254,6 +255,14 @@ NSURL * MKRUrlWithString(NSString *string){
         self.playerItem = [AVPlayerItem playerItemWithAsset:self.playAsset];
         self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
         [self.player addObserver:self forKeyPath:@"rate" options:NSKeyValueObservingOptionNew context:PlayerStatusObservationContext];
+        __weak __typeof(self)weakSelf = self;
+        self.timeObserver = [self.player addPeriodicTimeObserverForInterval:CMTimeMake(0.1*30, 30) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
+            CGFloat currentT = (CGFloat)CMTimeGetSeconds(time);
+            NSLog(@"当前时间--->%f",currentT);
+            if (weakSelf.currentStatus) {
+                weakSelf.currentStatus.currentDuration = currentT;
+            }
+        }];
     }
     if (@available(iOS 10.0, *)) {
         self.player.automaticallyWaitsToMinimizeStalling = NO;
@@ -413,7 +422,7 @@ NSURL * MKRUrlWithString(NSString *string){
     _currentStatus.totalTime = self.totalTime;
     _currentStatus.currentTime = self.currentTime;
     _currentStatus.totalDuration = self.totalTime;
-    _currentStatus.currentDuration = self.currentTime;
+//    _currentStatus.currentDuration = self.currentTime;
     _currentStatus.isPlaying = [self isPlaying];
     _currentStatus.playState = (MKRResourcePlayState)self.state;
     return _currentStatus;
@@ -562,6 +571,11 @@ NSURL * MKRUrlWithString(NSString *string){
     [_playerItem removeObserver:self forKeyPath:@"playbackBufferEmpty"];
     [_playerItem removeObserver:self forKeyPath:@"playbackLikelyToKeepUp"];
     [_playerItem removeObserver:self forKeyPath:@"duration"];
+    if (self.timeObserver) {
+        [self.player removeTimeObserver:self.timeObserver];
+        self.timeObserver = nil;
+    }
+    
 }
 
 @end