Browse Source

修复播放器audio engine 释放导致无法播放的问题

Steven 6 months ago
parent
commit
d8b4dbad64

+ 4 - 4
KulexiuForStudent/KulexiuForStudent.xcodeproj/project.pbxproj

@@ -309,7 +309,6 @@
 		BC1E28492C77151A0063E0A1 /* KSDeleteFailedAlertView.m in Sources */ = {isa = PBXBuildFile; fileRef = BC1E28482C77151A0063E0A1 /* KSDeleteFailedAlertView.m */; };
 		BC1E284B2C7715210063E0A1 /* KSDeleteFailedAlertView.xib in Resources */ = {isa = PBXBuildFile; fileRef = BC1E284A2C7715210063E0A1 /* KSDeleteFailedAlertView.xib */; };
 		BC1E28542C7839460063E0A1 /* KSToolLibrary.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC1E28532C7839460063E0A1 /* KSToolLibrary.framework */; };
-		BC1E28602C783BB70063E0A1 /* CloudAccompanyLibrary.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC1E285F2C783BB70063E0A1 /* CloudAccompanyLibrary.framework */; };
 		BC27A06E280FF56C00F91E27 /* AccompanyEvaluateCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = BC27A06A280FF56B00F91E27 /* AccompanyEvaluateCell.xib */; };
 		BC27A06F280FF56C00F91E27 /* AccompanyStudentEvaCell.m in Sources */ = {isa = PBXBuildFile; fileRef = BC27A06B280FF56C00F91E27 /* AccompanyStudentEvaCell.m */; };
 		BC27A070280FF56C00F91E27 /* AccompanyStudentEvaCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = BC27A06C280FF56C00F91E27 /* AccompanyStudentEvaCell.xib */; };
@@ -443,6 +442,7 @@
 		BC494A8028696BC300CCD343 /* MusicRoomListModel.m in Sources */ = {isa = PBXBuildFile; fileRef = BC494A7F28696BC300CCD343 /* MusicRoomListModel.m */; };
 		BC50171527FC0D8300F8BCBC /* SubjectChooseBodyView.m in Sources */ = {isa = PBXBuildFile; fileRef = BC50171427FC0D8300F8BCBC /* SubjectChooseBodyView.m */; };
 		BC50171727FC0D8E00F8BCBC /* SubjectChooseBodyView.xib in Resources */ = {isa = PBXBuildFile; fileRef = BC50171627FC0D8D00F8BCBC /* SubjectChooseBodyView.xib */; };
+		BC533DA62C7DFAB30073169B /* CloudAccompanyLibrary.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC533DA52C7DFAB30073169B /* CloudAccompanyLibrary.framework */; };
 		BC5367C2283F6D4D008428E8 /* HomeLiveCourseView.m in Sources */ = {isa = PBXBuildFile; fileRef = BC5367C1283F6D4C008428E8 /* HomeLiveCourseView.m */; };
 		BC5367C4283F6D58008428E8 /* HomeLiveCourseView.xib in Resources */ = {isa = PBXBuildFile; fileRef = BC5367C3283F6D58008428E8 /* HomeLiveCourseView.xib */; };
 		BC5367C7283F6D6B008428E8 /* HomeVideoCourseView.m in Sources */ = {isa = PBXBuildFile; fileRef = BC5367C6283F6D6B008428E8 /* HomeVideoCourseView.m */; };
@@ -1513,7 +1513,6 @@
 		BC1E28482C77151A0063E0A1 /* KSDeleteFailedAlertView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KSDeleteFailedAlertView.m; sourceTree = "<group>"; };
 		BC1E284A2C7715210063E0A1 /* KSDeleteFailedAlertView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = KSDeleteFailedAlertView.xib; sourceTree = "<group>"; };
 		BC1E28532C7839460063E0A1 /* KSToolLibrary.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = KSToolLibrary.framework; sourceTree = "<group>"; };
-		BC1E285F2C783BB70063E0A1 /* CloudAccompanyLibrary.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = CloudAccompanyLibrary.framework; sourceTree = "<group>"; };
 		BC255E822B29425F00A1FC27 /* SwiftImportHeader.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SwiftImportHeader.h; sourceTree = "<group>"; };
 		BC255E832B29425F00A1FC27 /* WebViewBaseConfig.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WebViewBaseConfig.h; sourceTree = "<group>"; };
 		BC27A068280FF56B00F91E27 /* AccompanyEvaluateCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AccompanyEvaluateCell.h; sourceTree = "<group>"; };
@@ -1742,6 +1741,7 @@
 		BC50171327FC0D8300F8BCBC /* SubjectChooseBodyView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SubjectChooseBodyView.h; sourceTree = "<group>"; };
 		BC50171427FC0D8300F8BCBC /* SubjectChooseBodyView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SubjectChooseBodyView.m; sourceTree = "<group>"; };
 		BC50171627FC0D8D00F8BCBC /* SubjectChooseBodyView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = SubjectChooseBodyView.xib; sourceTree = "<group>"; };
+		BC533DA52C7DFAB30073169B /* CloudAccompanyLibrary.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = CloudAccompanyLibrary.framework; sourceTree = "<group>"; };
 		BC5367C0283F6D4C008428E8 /* HomeLiveCourseView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HomeLiveCourseView.h; sourceTree = "<group>"; };
 		BC5367C1283F6D4C008428E8 /* HomeLiveCourseView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HomeLiveCourseView.m; sourceTree = "<group>"; };
 		BC5367C3283F6D58008428E8 /* HomeLiveCourseView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = HomeLiveCourseView.xib; sourceTree = "<group>"; };
@@ -2514,7 +2514,7 @@
 				BC8A45C5283DDE6D00094BBB /* AudioToolbox.framework in Frameworks */,
 				BC1E28542C7839460063E0A1 /* KSToolLibrary.framework in Frameworks */,
 				BC8A45C3283DDE5C00094BBB /* CoreMIDI.framework in Frameworks */,
-				BC1E28602C783BB70063E0A1 /* CloudAccompanyLibrary.framework in Frameworks */,
+				BC533DA62C7DFAB30073169B /* CloudAccompanyLibrary.framework in Frameworks */,
 				BC71D0FB2881A2420010F14B /* UShareUI.framework in Frameworks */,
 				BC71D0FD2881A2420010F14B /* libSocialWeChat.a in Frameworks */,
 				7ED0E7B1704A93EF8D10E5E6 /* libPods-KulexiuForStudent.a in Frameworks */,
@@ -4590,7 +4590,7 @@
 				BC00A6602BB58FDC00231B74 /* WMPlayer.bundle */,
 				BC3A55682BAA7B19002E1616 /* KSTunerLibrary.framework */,
 				BC1E28532C7839460063E0A1 /* KSToolLibrary.framework */,
-				BC1E285F2C783BB70063E0A1 /* CloudAccompanyLibrary.framework */,
+				BC533DA52C7DFAB30073169B /* CloudAccompanyLibrary.framework */,
 				BC3BF62F2B9EAFC800831494 /* client.p12 */,
 			);
 			path = ToolKit;

+ 27 - 26
KulexiuForStudent/KulexiuForStudent/Common/Base/AccompanyWebView/AudioEnginePlayer.m

@@ -30,8 +30,6 @@
 
 @property (nonatomic, assign) BOOL isPlayEnd;  // 是否播放完成
 
-@property (nonatomic, assign) BOOL isInterrupt; // 是否被打断
-
 @end
 
 @implementation AudioEnginePlayer
@@ -91,32 +89,29 @@
     AVAudioSessionInterruptionType type = [info[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
     if (type == AVAudioSessionInterruptionTypeBegan) {
         //Handle InterruptionBegan
-        if (self.isInterrupt == NO) {
-            if (self.bgPlayer.isPlaying) {
-                [self.bgPlayer stop];
-            }
-            self.isInterrupt = YES;
-            NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:99999 userInfo:@{@"errorDesc" : @"播放被打断"}];
-            [self sendInterruptError:error];
+        if (self.bgPlayer.isPlaying) {
+            [self.bgPlayer stop];
+            [self sendInterruptError:nil];
         }
     }
     else if (type == AVAudioSessionInterruptionTypeEnded) {
         AVAudioSessionInterruptionOptions options = [info[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];
-        if (options == AVAudioSessionInterruptionOptionShouldResume && self.isInterrupt) {
+        if (options == AVAudioSessionInterruptionOptionShouldResume) {
             //Handle Resume
-            
-            NSError *error = nil;
-            [[AVAudioSession sharedInstance] setActive:YES error:&error];
-            if (error) {
-                NSLog(@"------ error desc %@", error.description);
-            }
-            
+            [self resumeAudioSession];
             NSLog(@"---- 播放恢复");
-            self.isInterrupt = NO;
         }
     }
 }
 
+- (void)resumeAudioSession {
+    NSError *error = nil;
+    [[AVAudioSession sharedInstance] setActive:YES error:&error];
+    if (error) {
+        NSLog(@"------ error desc %@", error.description);
+    }
+}
+
 - (void)sendInterruptError:(NSError *)error {
     if (self.delegate && [self.delegate respondsToSelector:@selector(enginePlayerDidError:error:)]) {
         [self.delegate enginePlayerDidError:self error:error];
@@ -174,7 +169,21 @@
     });
 }
 
+- (void)retryInitEngine {
+    // 如果audio engine不存在
+    self.audioEngine = [[AVAudioEngine alloc] init];
+    // 重新添加节点
+    [self.audioEngine attachNode:self.bgPlayer];
+    [self.audioEngine attachNode:self.timePitchUnit];
+    // 重新建立链接
+    [self.audioEngine connect:self.bgPlayer to:self.timePitchUnit format:self.audioFormat];
+    [self.audioEngine connect:self.timePitchUnit to:self.audioEngine.mainMixerNode format:self.audioFormat];
+}
+
 - (void)startEngine {
+    if (!self.audioEngine) { // 如果audio engine 被释放
+        [self retryInitEngine];
+    }
     // 启动engine
     NSError *error = nil;
     @try {
@@ -190,10 +199,6 @@
 }
 
 - (void)startPlay {
-    if (self.isInterrupt) {
-        [self sendInterruptError:nil];
-        return;
-    }
     if (self.audioEngine.isRunning == NO) {
         [self startEngine];
     }
@@ -212,10 +217,6 @@
 
 // 从某个位置开始播放 ms
 - (void)seekToTimePlay:(NSInteger)time {
-    if (self.isInterrupt) {
-        [self sendInterruptError:nil];
-        return;
-    }
     if (self.audioEngine.isRunning == NO) {
         [self startEngine];
     }

+ 25 - 24
KulexiuForStudent/KulexiuForStudent/Common/MediaMerge/AudioMerge/MergePlayer/KSMergeEnginePlayer.m

@@ -41,8 +41,6 @@
 
 @property (nonatomic, assign) double sampleRate;
 
-@property (nonatomic, assign) BOOL isInterrupt; // 是否被打断
-
 @property (nonatomic, assign) NSInteger timeCount;
 
 @property (nonatomic, assign) BOOL isCanceled; // 是否在取消
@@ -92,8 +90,18 @@
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleRouteChange:) name:AVAudioSessionRouteChangeNotification object:audioSession];
 }
 
+- (void)retryInitEngine {
+    // 如果audio engine不存在
+    self.audioEngine = [[AVAudioEngine alloc] init];
+    [self.audioEngine attachNode:self.nodePlayer];
+    AVAudioFormat *outputFormat = [self.audioEngine.mainMixerNode outputFormatForBus:0];
+    [self.audioEngine connect:self.nodePlayer to:self.audioEngine.mainMixerNode format:outputFormat];
+}
 
 - (void)startEngine {
+    if (!self.audioEngine) { // 如果audio engine 被释放
+        [self retryInitEngine];
+    }
     // 启动engine
     NSError *error = nil;
     @try {
@@ -125,7 +133,6 @@
         
         AVAudioFormat *outputFormat = [self.audioEngine.mainMixerNode outputFormatForBus:0];
         [self.audioEngine connect:self.nodePlayer to:self.audioEngine.mainMixerNode format:outputFormat];
-//        [self addTapBus];
         [self startEngine];
         
         if (self.audioEngine && self.audioEngine.isRunning) {
@@ -348,31 +355,32 @@
     
     AVAudioSessionInterruptionType type = [info[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
     if (type == AVAudioSessionInterruptionTypeBegan) {
+        NSLog(@"---- 播放打断");
         //Handle InterruptionBegan
-        if (self.isInterrupt == NO) {
-            // 停止播放
-            if (self.nodePlayer.isPlaying) {
-                [self.nodePlayer stop];
-            }
-            self.isInterrupt = YES;
+        // 停止播放
+        if (self.nodePlayer.isPlaying) {
+            [self.nodePlayer stop];
             [self sendInterruptError:nil];
         }
     }
     else if (type == AVAudioSessionInterruptionTypeEnded) {
         AVAudioSessionInterruptionOptions options = [info[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue];
-        if (options == AVAudioSessionInterruptionOptionShouldResume && self.isInterrupt) {
+        if (options == AVAudioSessionInterruptionOptionShouldResume) {
             //Handle Resume
-            NSError *error = nil;
-            [[AVAudioSession sharedInstance] setActive:YES error:&error];
-            if (error) {
-                NSLog(@"------ error desc %@", error.description);
-            }
+            [self resumeAudioSession];
             NSLog(@"---- 播放恢复");
-            self.isInterrupt = NO;
         }
     }
 }
 
+- (void)resumeAudioSession {
+    NSError *error = nil;
+    [[AVAudioSession sharedInstance] setActive:YES error:&error];
+    if (error) {
+        NSLog(@"------ error desc %@", error.description);
+    }
+}
+
 - (void)handleRouteChange:(NSNotification *)notification {
     NSDictionary *info = notification.userInfo;
     AVAudioSessionRouteChangeReason reason = [info[AVAudioSessionRouteChangeReasonKey] unsignedIntegerValue];
@@ -405,10 +413,7 @@
 }
 
 - (void)seekToTimePlay:(NSInteger)time {
-    if (self.isInterrupt) {
-        [self sendInterruptError:nil];
-        return;
-    }
+
     if (self.audioEngine.isRunning == NO) {
         [self startEngine];
     }
@@ -430,10 +435,6 @@
 }
 
 - (void)seekToTime:(NSInteger)time {
-    if (self.isInterrupt) {
-        [self sendInterruptError:nil];
-        return;
-    }
     if (self.audioEngine.isRunning == NO) {
         [self startEngine];
     }

BIN
KulexiuForStudent/KulexiuForStudent/ToolKit/CloudAccompanyLibrary.framework/CloudAccompanyLibrary


BIN
KulexiuForStudent/KulexiuForStudent/ToolKit/CloudAccompanyLibrary.framework/Info.plist


BIN
KulexiuForStudent/KulexiuForStudent/ToolKit/CloudAccompanyLibrary.framework/_CodeSignature/CodeDirectory


BIN
KulexiuForStudent/KulexiuForStudent/ToolKit/CloudAccompanyLibrary.framework/_CodeSignature/CodeRequirements-1


+ 1 - 1
KulexiuForStudent/KulexiuForStudent/ToolKit/CloudAccompanyLibrary.framework/_CodeSignature/CodeResources

@@ -70,7 +70,7 @@
 		</data>
 		<key>Info.plist</key>
 		<data>
-		EbXK4oBtuwzpcM6n6jwLip0yy+c=
+		eWLlubwTo0FaMLs4jLsrdWyOWZs=
 		</data>
 	</dict>
 	<key>files2</key>

BIN
KulexiuForStudent/KulexiuForStudent/ToolKit/CloudAccompanyLibrary.framework/_CodeSignature/CodeSignature