diff --git a/Classes/AFSoundPlayback.h b/Classes/AFSoundPlayback.h index 5606409..7c0f756 100644 --- a/Classes/AFSoundPlayback.h +++ b/Classes/AFSoundPlayback.h @@ -33,6 +33,7 @@ typedef NS_ENUM(NSInteger, AFSoundStatus) { }; -(id)initWithItem:(AFSoundItem *)item; +-(id)initWithItem:(AFSoundItem *)item avAudioSessionCategory:(NSString *)categoryName; @property (nonatomic, strong) AVPlayer *player; @property (nonatomic) AFSoundStatus status; diff --git a/Classes/AFSoundPlayback.m b/Classes/AFSoundPlayback.m index 03e6117..b0be18d 100644 --- a/Classes/AFSoundPlayback.m +++ b/Classes/AFSoundPlayback.m @@ -39,6 +39,15 @@ -(id)initWithItem:(AFSoundItem *)item { return self; } +-(id)initWithItem:(AFSoundItem *)item avAudioSessionCategory:(NSString *)categoryName { + if (self == [super init]) { + _currentItem = item; + [self setUpItem:item avAudioSessionCategory:categoryName]; + _status = AFSoundStatusNotStarted; + } + return self; +} + -(void)setUpItem:(AFSoundItem *)item { _player = [[AVPlayer alloc] initWithURL:item.URL]; @@ -55,6 +64,12 @@ -(void)setUpItem:(AFSoundItem *)item { [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; } +-(void)setUpItem:(AFSoundItem *)item avAudioSessionCategory:(NSString *)categoryName { + [self setUpItem:item]; + [[AVAudioSession sharedInstance] setCategory:categoryName error:nil]; + [[AVAudioSession sharedInstance] setActive:YES error:nil]; +} + -(void)listenFeedbackUpdatesWithBlock:(feedbackBlock)block andFinishedBlock:(finishedBlock)finishedBlock { CGFloat updateRate = 1; diff --git a/Classes/AFSoundQueue.h b/Classes/AFSoundQueue.h index 8feca4b..6f4cf5c 100644 --- a/Classes/AFSoundQueue.h +++ b/Classes/AFSoundQueue.h @@ -17,6 +17,7 @@ typedef void (^feedbackBlock)(AFSoundItem *item); typedef void (^itemFinishedBlock)(AFSoundItem *nextItem); -(id)initWithItems:(NSArray *)items; +-(id)initWithItems:(NSArray *)items avAudioSessionCategory:(NSString *)categoryName; @property (nonatomic) AFSoundStatus status; diff --git a/Classes/AFSoundQueue.m b/Classes/AFSoundQueue.m index 49d1bba..51d2812 100644 --- a/Classes/AFSoundQueue.m +++ b/Classes/AFSoundQueue.m @@ -18,6 +18,7 @@ @interface AFSoundQueue () @property (nonatomic, strong) NSMutableArray *items; @property (nonatomic, strong) NSTimer *feedbackTimer; +@property (nonatomic, copy) NSString *avAudioSessionCategoryName; @end @@ -31,7 +32,11 @@ -(id)initWithItems:(NSArray *)items { _items = [NSMutableArray arrayWithArray:items]; - _queuePlayer = [[AFSoundPlayback alloc] initWithItem:items.firstObject]; + if (_avAudioSessionCategoryName) { + _queuePlayer = [[AFSoundPlayback alloc] initWithItem:items.firstObject avAudioSessionCategory:_avAudioSessionCategoryName]; + } else { + _queuePlayer = [[AFSoundPlayback alloc] initWithItem:items.firstObject]; + } [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; } @@ -40,6 +45,12 @@ -(id)initWithItems:(NSArray *)items { return self; } +-(id)initWithItems:(NSArray *)items avAudioSessionCategory:(NSString *)categoryName { + + _avAudioSessionCategoryName = categoryName; + return [self initWithItems:items]; +} + -(void)listenFeedbackUpdatesWithBlock:(feedbackBlock)block andFinishedBlock:(itemFinishedBlock)finishedBlock { CGFloat updateRate = 1; @@ -172,7 +183,11 @@ -(void)playItem:(AFSoundItem *)item { // [_feedbackTimer resumeTimer]; } - _queuePlayer = [[AFSoundPlayback alloc] initWithItem:item]; + if (_avAudioSessionCategoryName) { + _queuePlayer = [[AFSoundPlayback alloc] initWithItem:item avAudioSessionCategory:_avAudioSessionCategoryName]; + } else { + _queuePlayer = [[AFSoundPlayback alloc] initWithItem:item]; + } [_queuePlayer play]; [[MPRemoteCommandCenter sharedCommandCenter] playCommand];