Skip to content

Commit

Permalink
SGPlayer: Interruption.
Browse files Browse the repository at this point in the history
  • Loading branch information
libobjc committed Aug 14, 2019
1 parent 5863181 commit 7debac2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
4 changes: 1 addition & 3 deletions SGPlayer/Classes/SGPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ FOUNDATION_EXPORT const unsigned char SGPlayerVersionString[];

@property (nonatomic, strong) SGOptions *options;

@property (nonatomic) NSInteger tag;
@property (nonatomic, weak) id object;

- (NSError *)error;
- (SGTimeInfo)timeInfo;
- (SGStateInfo)stateInfo;
Expand Down Expand Up @@ -84,6 +81,7 @@ FOUNDATION_EXPORT const unsigned char SGPlayerVersionString[];
@property (nonatomic) Float64 rate;

@property (nonatomic, readonly) BOOL needsPlay;
@property (nonatomic) BOOL pausesWhenInterrupted;

- (BOOL)play;
- (BOOL)pause;
Expand Down
20 changes: 20 additions & 0 deletions SGPlayer/Classes/SGPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "SGPlayer.h"
#import <AVFoundation/AVFoundation.h>
#import "SGPlayerItem+Internal.h"
#import "SGRenderer+Internal.h"
#import "SGActivity.h"
Expand Down Expand Up @@ -58,6 +59,7 @@ - (instancetype)init
[self stop];
self->_options = [SGOptions sharedOptions].copy;
self->_rate = 1.0;
self->_pausesWhenInterrupted = YES;
self->_lock = [[NSLock alloc] init];
self->_clock = [[SGClock alloc] init];
self->_clock.delegate = self;
Expand All @@ -68,12 +70,17 @@ - (instancetype)init
self->_actionMask = SGInfoActionNone;
self->_minimumTimeInfoInterval = 1.0;
self->_notificationQueue = [NSOperationQueue mainQueue];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(interruptionHandler:)
name:AVAudioSessionInterruptionNotification
object:nil];
}
return self;
}

- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[SGActivity removeTarget:self];
[self->_currentItem close];
[self->_clock close];
Expand Down Expand Up @@ -655,4 +662,17 @@ + (SGInfoAction)infoActionFromUserInfo:(NSDictionary *)userInfo
return [userInfo[SGPlayerInfoActionUserInfoKey] unsignedIntegerValue];
}

#pragma mark - AVAudioSession

- (void)interruptionHandler:(NSNotification *)notification
{
if (self->_pausesWhenInterrupted == NO) {
return;
}
AVAudioSessionInterruptionType type = [notification.userInfo[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
if (type == AVAudioSessionInterruptionTypeBegan) {
[self pause];
}
}

@end

0 comments on commit 7debac2

Please sign in to comment.