Skip to content

Commit

Permalink
Merge pull request bilibili#24 from befovy/feature/audiosession
Browse files Browse the repository at this point in the history
feat: audio  interrupt handler in ijkffmediaplayer
  • Loading branch information
befovy authored May 3, 2020
2 parents 3de55d9 + 273f36a commit ca31d93
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 5 deletions.
1 change: 1 addition & 0 deletions ios/IJKMediaPlayer/IJKMediaPlayer/IJKAudioKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

+ (IJKAudioKit *)sharedInstance;
- (void)setupAudioSession;
- (void)setupAudioSessionWithoutInterruptHandler;
- (BOOL)setActive:(BOOL)active;

@end
8 changes: 6 additions & 2 deletions ios/IJKMediaPlayer/IJKMediaPlayer/IJKAudioKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,20 @@ - (void)setupAudioSession
object: [AVAudioSession sharedInstance]];
_audioSessionInitialized = YES;
}
[self setupAudioSessionWithoutInterruptHandler];
#endif
}

- (void)setupAudioSessionWithoutInterruptHandler
{
#if IJK_IOS
/* Set audio session to mediaplayback */
NSError *error = nil;
if (NO == [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error]) {
NSLog(@"IJKAudioKit: AVAudioSession.setCategory() failed: %@\n", error ? [error localizedDescription] : @"nil");
return;
}

[self setActive:YES];
return;
#endif
}

Expand Down
3 changes: 3 additions & 0 deletions ios/IJKMediaPlayer/IJKMediaPlayer/IJKFFMediaPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ typedef void(^OnSnapshotBlock) (UIImage* __nullable image , NSError* __nullable

@interface IJKFFMediaPlayer : NSObject<IJKCVPBViewProtocol, IJKSDLGLViewProtocol>


@property (nonatomic) BOOL ignoreAudioInterrupt;

- (instancetype)init;
- (instancetype)initWithFbo;
- (int) setDataSource:(NSString *)url;
Expand Down
55 changes: 52 additions & 3 deletions ios/IJKMediaPlayer/IJKMediaPlayer/IJKFFMediaPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#import "IJKFFMoviePlayerDef.h"
#import "IJKAudioKit.h"
#import "IJKFFOptions.h"
#import "IJKNotificationManager.h"
#import "ijkplayer/ijkplayer.h"

typedef NS_ENUM(NSInteger, IJKSDLFFPlayrRenderType) {
Expand All @@ -44,6 +45,7 @@ @implementation IJKFFMediaPlayer {
IjkMediaPlayer* _nativeMediaPlayer;
IJKFFMoviePlayerMessagePool *_msgPool;

IJKNotificationManager *_notificationManager;
NSMutableSet<id<IJKMPEventHandler>> *_eventHandlers;

CFDictionaryRef _optionsDictionary;
Expand All @@ -54,6 +56,7 @@ @implementation IJKFFMediaPlayer {
#endif
id<IJKCVPBViewProtocol> _cvPBView;
IJKSDLFFPlayrRenderType _renderType;
BOOL _playingBeforeInterruption;
}


Expand Down Expand Up @@ -133,12 +136,16 @@ - (void) nativeSetup
ijkmp_set_inject_opaque(_nativeMediaPlayer, (__bridge_retained void *) weakHolder);
ijkmp_set_ijkio_inject_opaque(_nativeMediaPlayer, (__bridge_retained void *) weakHolder);

[[IJKAudioKit sharedInstance] setupAudioSession];
_notificationManager = [[IJKNotificationManager alloc] init];

[[IJKAudioKit sharedInstance] setupAudioSessionWithoutInterruptHandler];
_optionsDictionary = nil;
_isThirdGLView = true;
_scaleFactor = 1.0f;
_fps = 1.0f;

[self registerApplicationObservers];

}

- (void)postEvent: (IJKFFMoviePlayerMessage *)msg
Expand Down Expand Up @@ -229,7 +236,6 @@ - (int64_t) getLongProperty:(int) property default:(int64_t) value
return ijkmp_get_property_int64(_nativeMediaPlayer, property, value);
}


- (void)setPlaybackVolume:(float)volume
{
if (!_nativeMediaPlayer)
Expand All @@ -246,7 +252,9 @@ - (float)playbackVolume

- (void) shutdown
{
_ignoreAudioInterrupt = YES;
ijkmp_shutdown(_nativeMediaPlayer);
[self unregisterApplicationObservers];

__unused id weakPlayer = (__bridge_transfer IJKFFMediaPlayer*)ijkmp_set_weak_thiz(_nativeMediaPlayer, NULL);
__unused id weakHolder = (__bridge_transfer IJKFFWeakHolder*)ijkmp_set_inject_opaque(_nativeMediaPlayer, NULL);
Expand All @@ -257,7 +265,7 @@ - (void) shutdown

[_eventHandlers removeAllObjects];
ijkmp_dec_ref_p(&_nativeMediaPlayer);

_cvPBView = nil;
#if IJK_IOS
_fboView = nil;
Expand Down Expand Up @@ -411,5 +419,46 @@ - (void) takeSnapshot:(OnSnapshotBlock) block
_onSnapshot = block;
}

- (void)registerApplicationObservers
{
[_notificationManager addObserver:self
selector:@selector(audioSessionInterrupt:)
name:AVAudioSessionInterruptionNotification
object:nil];
}

- (void)unregisterApplicationObservers
{
[_notificationManager removeAllObservers:self];
}


- (void)audioSessionInterrupt:(NSNotification *)notification
{
if (_ignoreAudioInterrupt) {
return;
}
int reason = [[[notification userInfo] valueForKey:AVAudioSessionInterruptionTypeKey] intValue];
switch (reason) {
case AVAudioSessionInterruptionTypeBegan: {
if (_nativeMediaPlayer && ijkmp_get_state(_nativeMediaPlayer) == MP_STATE_STARTED) {
_playingBeforeInterruption = YES;
} else{
_playingBeforeInterruption = NO;
}
NSLog(@"IJKFFMediaPlayer:audioSessionInterrupt: begin, %d\n", _playingBeforeInterruption);
[self pause];
[[IJKAudioKit sharedInstance] setActive:NO];
break;
}
case AVAudioSessionInterruptionTypeEnded: {
NSLog(@"IJKFFMediaPlayer:audioSessionInterrupt: end\n");
[[IJKAudioKit sharedInstance] setActive:YES];
if (_playingBeforeInterruption) {
[self start];
}
break;
}
}
}
@end

0 comments on commit ca31d93

Please sign in to comment.