Selaa lähdekoodia

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

Steven 10 kuukautta sitten
vanhempi
commit
eb77b030f7

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

@@ -386,7 +386,6 @@
 		BC1E284F2C77218A0063E0A1 /* KSDeleteFailedAlertView.xib in Resources */ = {isa = PBXBuildFile; fileRef = BC1E284C2C7721890063E0A1 /* KSDeleteFailedAlertView.xib */; };
 		BC1E28502C77218A0063E0A1 /* KSDeleteFailedAlertView.m in Sources */ = {isa = PBXBuildFile; fileRef = BC1E284E2C77218A0063E0A1 /* KSDeleteFailedAlertView.m */; };
 		BC1E285A2C783B5A0063E0A1 /* KSToolLibrary.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC1E28592C783B5A0063E0A1 /* KSToolLibrary.framework */; };
-		BC1E285E2C783B940063E0A1 /* CloudAccompanyLibrary.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC1E285D2C783B940063E0A1 /* CloudAccompanyLibrary.framework */; };
 		BC221FA128C72B9500F99802 /* MyStyleVideoView.xib in Resources */ = {isa = PBXBuildFile; fileRef = BC221F9D28C72B9500F99802 /* MyStyleVideoView.xib */; };
 		BC221FA228C72B9500F99802 /* MyStyleVideoCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = BC221F9E28C72B9500F99802 /* MyStyleVideoCell.xib */; };
 		BC221FA328C72B9500F99802 /* MyStyleVideoView.m in Sources */ = {isa = PBXBuildFile; fileRef = BC221F9F28C72B9500F99802 /* MyStyleVideoView.m */; };
@@ -562,6 +561,7 @@
 		BC513E7E28A4D874003F58C4 /* live_animation.json in Resources */ = {isa = PBXBuildFile; fileRef = BC513E7D28A4D874003F58C4 /* live_animation.json */; };
 		BC523B1028A09F3000E9D66B /* MineHeadView.m in Sources */ = {isa = PBXBuildFile; fileRef = BC523B0F28A09F2F00E9D66B /* MineHeadView.m */; };
 		BC523B1228A09F3800E9D66B /* MineHeadView.xib in Resources */ = {isa = PBXBuildFile; fileRef = BC523B1128A09F3800E9D66B /* MineHeadView.xib */; };
+		BC533DA42C7DF2E50073169B /* CloudAccompanyLibrary.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC533DA32C7DF2E50073169B /* CloudAccompanyLibrary.framework */; };
 		BC542E3328406EA400633781 /* UserAuthViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = BC542E3228406EA400633781 /* UserAuthViewController.m */; };
 		BC542E3628406F7700633781 /* UserAuthBodyView.m in Sources */ = {isa = PBXBuildFile; fileRef = BC542E3528406F7700633781 /* UserAuthBodyView.m */; };
 		BC542E3828406F8000633781 /* UserAuthBodyView.xib in Resources */ = {isa = PBXBuildFile; fileRef = BC542E3728406F8000633781 /* UserAuthBodyView.xib */; };
@@ -1798,7 +1798,6 @@
 		BC1E284D2C7721890063E0A1 /* KSDeleteFailedAlertView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KSDeleteFailedAlertView.h; sourceTree = "<group>"; };
 		BC1E284E2C77218A0063E0A1 /* KSDeleteFailedAlertView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KSDeleteFailedAlertView.m; sourceTree = "<group>"; };
 		BC1E28592C783B5A0063E0A1 /* KSToolLibrary.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = KSToolLibrary.framework; sourceTree = "<group>"; };
-		BC1E285D2C783B940063E0A1 /* CloudAccompanyLibrary.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = CloudAccompanyLibrary.framework; sourceTree = "<group>"; };
 		BC221F9B28C72B9500F99802 /* MyStyleVideoView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MyStyleVideoView.h; sourceTree = "<group>"; };
 		BC221F9C28C72B9500F99802 /* MyStyleVideoCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MyStyleVideoCell.h; sourceTree = "<group>"; };
 		BC221F9D28C72B9500F99802 /* MyStyleVideoView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = MyStyleVideoView.xib; sourceTree = "<group>"; };
@@ -2082,6 +2081,7 @@
 		BC523B0E28A09F2F00E9D66B /* MineHeadView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MineHeadView.h; sourceTree = "<group>"; };
 		BC523B0F28A09F2F00E9D66B /* MineHeadView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MineHeadView.m; sourceTree = "<group>"; };
 		BC523B1128A09F3800E9D66B /* MineHeadView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = MineHeadView.xib; sourceTree = "<group>"; };
+		BC533DA32C7DF2E50073169B /* CloudAccompanyLibrary.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = CloudAccompanyLibrary.framework; sourceTree = "<group>"; };
 		BC542E3128406EA400633781 /* UserAuthViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UserAuthViewController.h; sourceTree = "<group>"; };
 		BC542E3228406EA400633781 /* UserAuthViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = UserAuthViewController.m; sourceTree = "<group>"; };
 		BC542E3428406F7700633781 /* UserAuthBodyView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UserAuthBodyView.h; sourceTree = "<group>"; };
@@ -2962,7 +2962,7 @@
 				2779333F27E314640010E277 /* CoreVideo.framework in Frameworks */,
 				BC8B6E572856ED0600866917 /* UMDevice.framework in Frameworks */,
 				BC3A55662BAA799B002E1616 /* KSTunerLibrary.framework in Frameworks */,
-				BC1E285E2C783B940063E0A1 /* CloudAccompanyLibrary.framework in Frameworks */,
+				BC533DA42C7DF2E50073169B /* CloudAccompanyLibrary.framework in Frameworks */,
 				2779334127E3146B0010E277 /* CoreMedia.framework in Frameworks */,
 				2779333D27E314550010E277 /* AVFoundation.framework in Frameworks */,
 				2779333B27E314450010E277 /* AudioToolbox.framework in Frameworks */,
@@ -5254,7 +5254,7 @@
 			children = (
 				BC3A55612BAA798A002E1616 /* KSTunerLibrary.framework */,
 				BC1E28592C783B5A0063E0A1 /* KSToolLibrary.framework */,
-				BC1E285D2C783B940063E0A1 /* CloudAccompanyLibrary.framework */,
+				BC533DA32C7DF2E50073169B /* CloudAccompanyLibrary.framework */,
 				BC00A65B2BB58F1700231B74 /* LLPhotoBrowse.bundle */,
 				BC00A6592BB58F0000231B74 /* WMPlayer.bundle */,
 				BC24570E286C437D00D1F7C0 /* SoundFontFile */,

+ 27 - 26
KulexiuForTeacher/KulexiuForTeacher/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
KulexiuForTeacher/KulexiuForTeacher/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
KulexiuForTeacher/KulexiuForTeacher/ToolKit/CloudAccompanyLibrary.framework/CloudAccompanyLibrary


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


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


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


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

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

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