Skip to content

Commit bb11da1

Browse files
authored
Merge pull request #5 from sfmDev/develop
Develop
2 parents 1a2492a + 1d5c03b commit bb11da1

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

Example/LPDSoundServiceKit/LPDAppDelegate.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
//
88

99
#import "LPDAppDelegate.h"
10+
#import <LPDSoundServiceKit/LPDSoundServiceKit.h>
1011

1112
@implementation LPDAppDelegate
1213

1314
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
1415
{
1516
// Override point for customization after application launch.
16-
1717
return YES;
1818
}
1919

Example/LPDSoundServiceKit/LPDViewController.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ @implementation LPDViewController
1717

1818
- (void)viewDidLoad {
1919
[super viewDidLoad];
20+
[[LPDSoundService sharedInstance] setupNotifications];
2021
}
2122

2223
- (IBAction)playSoundTapped:(UIButton *)sender {

LPDSoundServiceKit.podspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Pod::Spec.new do |s|
1010
s.name = 'LPDSoundServiceKit'
11-
s.version = '0.4.6'
11+
s.version = '0.4.7'
1212
s.summary = 'A manager of AVAudiosession for play sound.'
1313

1414
# This description is used to generate tags and improve search results.
@@ -25,7 +25,7 @@ TODO: Add long description of the pod here.
2525
# s.screenshots = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
2626
s.license = { :type => 'MIT', :file => 'LICENSE' }
2727
s.author = { '[email protected]' => '[email protected]' }
28-
s.source = { :git => 'https://github.com/LPD-iOS/LPDSoundServiceKit.git', :tag => s.version.to_s }
28+
s.source = { :git => 'https://github.com/sfmDev/LPDSoundServiceKit.git', :tag => s.version.to_s }
2929
# s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
3030

3131
s.ios.deployment_target = '8.0'

LPDSoundServiceKit/Classes/LPDSoundService.m

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ @interface LPDSoundService () <AVAudioPlayerDelegate>
2121
@property (nonatomic, strong) NSMutableArray<NSString *> *cacheSounds;
2222
@property (nonatomic, assign) BOOL isPlaying;
2323
@property (nonatomic, assign) BOOL needCache;
24+
@property (nonatomic, assign) BOOL isInterrupt;
2425

2526
@end
2627

@@ -38,17 +39,19 @@ + (instancetype)sharedInstance {
3839
- (instancetype)init {
3940
self = [super init];
4041
if (self) {
41-
[LPDTeleponyManager sharedInstance];
4242
AVAudioSession *session = [AVAudioSession sharedInstance];
43-
[session setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:AVAudioSessionCategoryOptionDuckOthers | AVAudioSessionCategoryOptionAllowBluetooth error:nil];
43+
[session setCategory:AVAudioSessionCategoryPlayback withOptions: AVAudioSessionCategoryOptionDuckOthers | AVAudioSessionCategoryOptionAllowBluetooth error:nil];
4444

4545
self.cacheSounds = [NSMutableArray array];
4646
self.canShake = YES;
4747
self.needCache = YES;
48-
[self setupNotifications];
4948
[[NSNotificationCenter defaultCenter] addObserver:self
5049
selector:@selector(playCacheSoundWhenApplicationDidBecomeActive)
5150
name:UIApplicationDidBecomeActiveNotification object:nil];
51+
[[NSNotificationCenter defaultCenter] addObserver:self
52+
selector:@selector(interrupted:)
53+
name:AVAudioSessionInterruptionNotification
54+
object: [AVAudioSession sharedInstance]];
5255
}
5356
return self;
5457
}
@@ -69,15 +72,12 @@ - (void)playSoundWithName:(NSString *)soundName ofType:(NSString*)type {
6972
}
7073
return;
7174
}
72-
if (self.needCache == YES) {
73-
self.isPlaying = YES;
74-
}
7575

7676
//让app支持接受远程控制事件
7777
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
7878

7979
[self setOutputWith: [AVAudioSession sharedInstance]];
80-
[[AVAudioSession sharedInstance] setActive:YES error:nil];
80+
[[AVAudioSession sharedInstance]setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
8181

8282
NSURL *fileURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:soundName ofType:type]];
8383
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
@@ -131,9 +131,12 @@ - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)f
131131
[self.audioPlayer stop];
132132
self.audioPlayer = nil;
133133
NSLog(@"sound finish");
134+
NSError *error;
134135
[[AVAudioSession sharedInstance] setActive:NO
135-
withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation
136-
error:nil];
136+
error:&error];
137+
if (error) {
138+
NSLog(@"%@",error.localizedDescription);
139+
}
137140
if (self.isPlaying) {
138141
self.isPlaying = NO;
139142
[self playCacheSound];
@@ -168,27 +171,25 @@ - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)
168171
}
169172
}
170173

171-
- (void)setupNotifications {
172-
[[NSNotificationCenter defaultCenter] addObserver:self
173-
selector:@selector(interrupted:)
174-
name:AVAudioSessionInterruptionNotification
175-
object: [AVAudioSession sharedInstance]];
176-
}
177-
178174
-(void)interrupted:(NSNotification *)notification {
179175
NSDictionary *userInfo = notification.userInfo;
180-
if (userInfo.count > 0) {
181-
if ([[userInfo objectForKey:AVAudioSessionSilenceSecondaryAudioHintTypeKey] isKindOfClass:[NSNumber class]]) {
182-
AVAudioSessionSilenceSecondaryAudioHintType typeValue = [[userInfo objectForKey:AVAudioSessionSilenceSecondaryAudioHintTypeKey] integerValue];
183-
if (typeValue == AVAudioSessionSilenceSecondaryAudioHintTypeBegin) {
184-
if([self.audioPlayer isPlaying]) {
185-
[self.audioPlayer pause];
186-
}
187-
} else {
176+
if (!userInfo) { return; }
177+
178+
if ([[userInfo objectForKey:AVAudioSessionInterruptionTypeKey] isKindOfClass:[NSNumber class]]) {
179+
AVAudioSessionInterruptionType typeValue = [[userInfo objectForKey:AVAudioSessionInterruptionTypeKey] integerValue];
180+
if (typeValue == AVAudioSessionInterruptionTypeBegan) {
181+
self.isInterrupt = YES;
182+
if([self.audioPlayer isPlaying]) {
183+
[self.audioPlayer pause];
184+
}
185+
} else {
186+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
187+
NSLog(@"playing");
188188
if (![self.audioPlayer isPlaying]) {
189189
[self.audioPlayer play];
190190
}
191-
}
191+
self.isInterrupt = NO;
192+
});
192193
}
193194
}
194195
}

0 commit comments

Comments
 (0)