Browse Source

播放器优化

Steven 6 months ago
parent
commit
101657927c

+ 1 - 3
KulexiuForStudent/KulexiuForStudent/Common/MediaMerge/AudioMerge/KSMediaMergeView.m

@@ -886,9 +886,7 @@
 
 - (void)freePlayer {
     if (_mergePlayer) {
-        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
-            [self.mergePlayer freePlayer];
-        });
+        [self.mergePlayer freePlayer];
     }
     if (_videoView) {
         [self.videoView freePlayer];

+ 29 - 7
KulexiuForStudent/KulexiuForStudent/Common/MediaMerge/AudioMerge/MergePlayer/KSMergeEnginePlayer.m

@@ -45,6 +45,8 @@
 
 @property (nonatomic, assign) NSInteger timeCount;
 
+@property (nonatomic, assign) BOOL isCanceled; // 是否在取消
+
 @end
 
 
@@ -110,8 +112,12 @@
 }
 
 - (void)prepareNativeSongWithUrl:(NSURL *)recordAudioUrl bgMusic:(NSURL *)bgMusicUrl {
+    @weakObj(self);
     dispatch_async(self.sourceQueue, ^{
-        
+        @strongObj(self);
+        if (!self || self.isCanceled) {
+            return;
+        }
         [self loadAuidoFile:recordAudioUrl isBgm:NO];
         [self loadAuidoFile:bgMusicUrl isBgm:YES];
         self.sampleRate = self.audioFile.fileFormat.sampleRate;
@@ -205,7 +211,12 @@
 // 预计加载buffer
 - (void)prepareBuffer:(AVAudioFramePosition)startPosition offset:(NSInteger)offsetTime mixStart:(AVAudioFramePosition)mixStartPosition {
     
+    @weakObj(self);
     dispatch_async(self.sourceQueue, ^{
+        @strongObj(self);
+        if (!self || self.isCanceled) {
+            return;
+        }
         if (!self.bgAudioFile || !self.audioFile) {
             return;
         }
@@ -408,11 +419,15 @@
 }
 
 - (void)stopPlay {
-
-    if (self.nodePlayer.isPlaying) {
-        [self.nodePlayer stop];
-    }
-    [self stopTimer];
+    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
+        if (self.nodePlayer.isPlaying) {
+            [self.nodePlayer stop];
+        }
+        [self stopTimer];
+        if (self.audioEngine.isRunning) {
+            [self.audioEngine stop];
+        }
+    });
 }
 
 - (void)seekToTime:(NSInteger)time {
@@ -457,8 +472,8 @@
 }
 
 - (void)freePlayer {
+    self.isCanceled = YES;
     [self stopPlay];
-    [self.audioEngine.mainMixerNode removeTapOnBus:0];
 }
 
 - (void)startTimer {
@@ -493,6 +508,9 @@
 }
 
 - (void)timeFunction {
+    if (self.isCanceled) {
+        return;
+    }
     self.totalDuration = [self getTotalTime];
     NSTimeInterval currentTime = [self getCurrentPlayTime];
     float progress = currentTime/self.totalDuration;
@@ -566,6 +584,10 @@
 
 - (void)dealloc {
     NSLog(@"---- KSMergeEnginePlayer dealloc");
+    if (_audioEngine) {
+        [_audioEngine disconnectNodeInput:self.nodePlayer];
+        [_audioEngine detachNode:self.nodePlayer];
+    }
     // 停止并清理定时器
     if (_timer) {
         [_timer invalidate];