Skip to content

Commit

Permalink
SGPlayer: Support auto-pause when enter background.
Browse files Browse the repository at this point in the history
  • Loading branch information
libobjc committed Sep 19, 2019
1 parent 75abf9a commit 3164515
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 10 deletions.
4 changes: 2 additions & 2 deletions SGPlayer/Classes/Core/SGRenderer/SGClock.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ - (void)setAudioTime:(CMTime)time running:(BOOL)running

- (void)setVideoTime:(CMTime)time
{
SGLockCondEXE00(self->_lock, ^BOOL{
SGLockCondEXE00(self->_lock, ^BOOL {
return self->_audioRunning == NO && CMTIME_IS_NUMERIC(time);
}, ^{
if (self->_videoRunning == NO) {
Expand All @@ -107,7 +107,7 @@ - (void)setVideoTime:(CMTime)time

- (BOOL)open
{
return SGLockCondEXE00(self->_lock, ^BOOL{
return SGLockCondEXE00(self->_lock, ^BOOL {
return self->_audioTimebase == NULL;
}, ^{
CMTimebaseCreateWithMasterClock(NULL, self->_masterClock, &self->_playbackTimebase);
Expand Down
4 changes: 2 additions & 2 deletions SGPlayer/Classes/Core/SGRenderer/SGVideoRenderer.m
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ - (void)drawInMTKView:(MTKView *)view

- (void)mtkView:(MTKView *)view drawableSizeWillChange:(CGSize)size
{
SGLockCondEXE10(self->_lock, ^BOOL{
SGLockCondEXE10(self->_lock, ^BOOL {
return
self->_flags.state == SGRenderableStateRendering ||
self->_flags.state == SGRenderableStatePaused ||
Expand Down Expand Up @@ -528,7 +528,7 @@ - (void)setDisplayMode:(SGDisplayMode)displayMode
{
if (self->_displayMode != displayMode) {
self->_displayMode = displayMode;
SGLockCondEXE10(self->_lock, ^BOOL{
SGLockCondEXE10(self->_lock, ^BOOL {
return
self->_displayMode != SGDisplayModePlane &&
(self->_flags.state == SGRenderableStateRendering ||
Expand Down
2 changes: 1 addition & 1 deletion SGPlayer/Classes/Core/SGSession/SGPacketOutput.m
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ - (void)runningThread
SGPacket *packet = nil;
NSError *error = [self->_demuxable nextPacket:&packet];
if (error) {
SGLockCondEXE10(self->_lock, ^BOOL{
SGLockCondEXE10(self->_lock, ^BOOL {
return self->_flags.state == SGPacketOutputStateReading;
}, ^SGBlock{
return [self setState:SGPacketOutputStateFinished];
Expand Down
19 changes: 18 additions & 1 deletion SGPlayer/Classes/SGPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,26 @@
/*!
@property pausesWhenInterrupted
@abstract
Indicates whether to automatically pause whan audio session is interrupted.
Indicates whether to automatically pause when audio session is interrupted.
Default is YES.
*/
@property (nonatomic) BOOL pausesWhenInterrupted;

/*!
@property pausesWhenEnteredBackground
@abstract
Indicates whether to automatically pause when application did enter background.
Default is YES.
*/
@property (nonatomic) BOOL pausesWhenEnteredBackground;

/*!
@property pausesWhenEnteredBackgroundIfNoAudioTrack
@abstract
Indicates whether to automatically pause when application did enter background if no audio track.
Default is YES.
*/
@property (nonatomic) BOOL pausesWhenEnteredBackgroundIfNoAudioTrack;
#endif

/*!
Expand Down
27 changes: 23 additions & 4 deletions SGPlayer/Classes/SGPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#import "SGMacro.h"
#import "SGLock.h"

#if SGPLATFORM_TARGET_OS_IPHONE_OR_TV
#import <UIKit/UIKit.h>
#endif

NSString * const SGPlayerTimeInfoUserInfoKey = @"SGPlayerTimeInfoUserInfoKey";
NSString * const SGPlayerStateInfoUserInfoKey = @"SGPlayerStateInfoUserInfoKey";
NSString * const SGPlayerInfoActionUserInfoKey = @"SGPlayerInfoActionUserInfoKey";
Expand Down Expand Up @@ -71,7 +75,10 @@ - (instancetype)init
self->_notificationQueue = [NSOperationQueue mainQueue];
#if SGPLATFORM_TARGET_OS_IPHONE_OR_TV
self->_pausesWhenInterrupted = YES;
self->_pausesWhenEnteredBackground = YES;
self->_pausesWhenEnteredBackgroundIfNoAudioTrack = YES;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(interruptionHandler:) name:AVAudioSessionInterruptionNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterBackgroundHandler:) name:UIApplicationDidEnterBackgroundNotification object:nil];
#endif
}
return self;
Expand Down Expand Up @@ -637,12 +644,24 @@ + (SGInfoAction)infoActionFromUserInfo:(NSDictionary *)userInfo
#if SGPLATFORM_TARGET_OS_IPHONE_OR_TV
- (void)interruptionHandler:(NSNotification *)notification
{
if (self->_pausesWhenInterrupted == NO) {
return;
if (self->_pausesWhenInterrupted == YES) {
AVAudioSessionInterruptionType type = [notification.userInfo[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
if (type == AVAudioSessionInterruptionTypeBegan) {
[self pause];
}
}
AVAudioSessionInterruptionType type = [notification.userInfo[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
if (type == AVAudioSessionInterruptionTypeBegan) {
}

- (void)enterBackgroundHandler:(NSNotification *)notification
{
if (self->_pausesWhenEnteredBackground) {
[self pause];
} else if (self->_pausesWhenEnteredBackgroundIfNoAudioTrack) {
SGLockCondEXE11(self->_lock, ^BOOL {
return self->_flags.audioAvailable == NO && self->_flags.videoAvailable == YES;
}, nil, ^BOOL(SGBlock block) {
return [self pause];
});
}
}
#endif
Expand Down

0 comments on commit 3164515

Please sign in to comment.