Browse Source

播放器添加异常监听

Steven 1 year ago
parent
commit
177e67c494

+ 8 - 0
KulexiuForTeacher/KulexiuForTeacher/Common/Base/KSAccompanyWebViewController.m

@@ -1698,6 +1698,7 @@
     }
 }
 
+
 - (void)sendPlayerReadyMsg {
     if (self.playerParm) {
         [self postMessage:self.playerParm];
@@ -1721,6 +1722,13 @@
     }
 }
 
+- (void)playerDidError:(kSNewPlayer *)player {
+    // 播放出现问题
+    NSDictionary *postParm = @{@"api" : @"cancelEvaluating",
+                               @"content" : @{@"reson":@"播放已停止"}
+    };
+    [self postMessage:postParm];
+}
 
 - (NSMutableArray *)delayArray {
     if (!_delayArray) {

+ 3 - 0
KulexiuForTeacher/KulexiuForTeacher/Common/MediaMerge/AudioMerge/KSMediaMergeView.m

@@ -1042,6 +1042,9 @@
     }
 }
 
+- (void)playerDidError:(kSNewPlayer *)player {
+    [self stopPlay];
+}
 
 - (KSVideoPlayerView *)videoView {
     if (!_videoView) {

+ 2 - 0
KulexiuForTeacher/KulexiuForTeacher/Common/MediaMerge/MusicPlayer/kSNewPlayer.h

@@ -24,6 +24,8 @@
 
 - (void)playerIsReadyPlay:(kSNewPlayer *_Nonnull)player;
 
+- (void)playerDidError:(kSNewPlayer *_Nonnull)player;
+
 @end
 
 NS_ASSUME_NONNULL_BEGIN

+ 26 - 9
KulexiuForTeacher/KulexiuForTeacher/Common/MediaMerge/MusicPlayer/kSNewPlayer.m

@@ -22,10 +22,6 @@
 
 @property (nonatomic, assign) BOOL hasFreeObserver;
 
-@property (nonatomic, assign) BOOL isSeekInProgress;
-
-@property (nonatomic, assign) CMTime chaseTime;
-
 @end
 
 @implementation kSNewPlayer
@@ -119,6 +115,8 @@
         [self addTimeObserve];
         //添加播放完成的通知
         [self addPlayToEndObserver];
+        // 添加异常通知
+        [self addExceptionObserver];
     }
 }
 - (void)configPlayerRate:(CGFloat)rate {
@@ -249,23 +247,25 @@
 
 -(void)addTimeObserve
 {
-    __block typeof(self) bself = self;
+//    __block typeof(self) bself = self;
+    @weakObj(self);
     _timeObserver = [_player addPeriodicTimeObserverForInterval:CMTimeMake(1, 100) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
+        @strongObj(self);
         //设置player的声音
-        //        [bself setPlayerVolume];
+        //        [self setPlayerVolume];
         //当前时间
         float current = CMTimeGetSeconds(time);
         //总共时间
-        float total = CMTimeGetSeconds(bself.player.currentItem.asset.duration);
+        float total = CMTimeGetSeconds(self.player.currentItem.asset.duration);
         //进度
         float progress = current/total;
         //将值传入知道delegate方法中
         //        NSLog(@"--current %f -- total %f -----%f", current, total, progress);
-        if (bself.delegate && [bself.delegate respondsToSelector:@selector(getSongCurrentTime:andTotalTime:andProgress:currentInterval:playTime:inPlayer:)]) {
+        if (self.delegate && [self.delegate respondsToSelector:@selector(getSongCurrentTime:andTotalTime:andProgress:currentInterval:playTime:inPlayer:)]) {
             NSDate *date = [NSDate date];
             NSTimeInterval inteveral = [date timeIntervalSince1970];
             //            NSLog(@"----- start play %f", inteveral * 1000);
-            [bself.delegate getSongCurrentTime:current*1000  andTotalTime:total*1000 andProgress:progress currentInterval:inteveral playTime:current*1000 inPlayer:bself];
+            [self.delegate getSongCurrentTime:current*1000  andTotalTime:total*1000 andProgress:progress currentInterval:inteveral playTime:current*1000 inPlayer:self];
         }
     }];
 }
@@ -376,6 +376,23 @@
 {
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:_songItem];
 }
+#pragma mark ---- 异常通知
+- (void)addExceptionObserver {
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerError:) name:AVPlayerItemFailedToPlayToEndTimeNotification object:_songItem];
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerError:) name:AVPlayerItemPlaybackStalledNotification object:_songItem];
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerError:) name:AVPlayerItemPlaybackStalledNotification object:_songItem];
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerError:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerError:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];
+}
+
+-(void)playerError:(NSNotification *)notice {
+    //移除所有监听
+    dispatch_main_sync_safe(^{
+        if ([self.delegate respondsToSelector:@selector(playerDidError:)]) {
+            [self.delegate playerDidError:self];
+        }
+    });
+}
 
 #pragma mark---通知的方法
 -(void)playFinished:(NSNotification *)notice