From 128177371ffd5e8ff763c59a5b9945b40958aa36 Mon Sep 17 00:00:00 2001 From: Juanpe Date: Tue, 14 Jul 2015 18:18:07 +0200 Subject: [PATCH] Add APAudioPlayerState Add Audio Streaming --- APAudioPlayer/APAudioPlayer.h | 21 ++++++- APAudioPlayer/APAudioPlayer.m | 110 +++++++++++++++++++++++++++------- 2 files changed, 106 insertions(+), 25 deletions(-) diff --git a/APAudioPlayer/APAudioPlayer.h b/APAudioPlayer/APAudioPlayer.h index ec048a7..074f03e 100644 --- a/APAudioPlayer/APAudioPlayer.h +++ b/APAudioPlayer/APAudioPlayer.h @@ -8,10 +8,28 @@ #import +typedef NS_ENUM(NSInteger, APAudioPlayerState) { + APAudioPlayerStateStopped, + APAudioPlayerStatePlaying, + APAudioPlayerStatePaused, + APAudioPlayerStateError, + APAudioPlayerStateBuffering +}; + @protocol APAudioPlayerDelegate; @interface APAudioPlayer : NSObject @property (nonatomic, weak) id delegate; +@property (assign, nonatomic, readonly) APAudioPlayerState currentState; + +/** + * Prepares player to play item + * + * @param urlPath NSURL path of the track + * @param autoplay BOOL is should play immidiately + * + */ +- (void)loadItemWithPath:(NSURL *)urlPath autoPlay:(BOOL)autoplay; /** * Prepares player to play item @@ -19,9 +37,8 @@ * @param url NSURL of the track * @param autoplay BOOL is should play immidiately * - * @return BOOL. Represents success status */ -- (BOOL)loadItemWithURL:(NSURL *)url autoPlay:(BOOL)autoplay; +- (void)loadItemWithURL:(NSURL *)url autoPlay:(BOOL)autoplay; /* Player interactions diff --git a/APAudioPlayer/APAudioPlayer.m b/APAudioPlayer/APAudioPlayer.m index a7626b5..b59b98f 100644 --- a/APAudioPlayer/APAudioPlayer.m +++ b/APAudioPlayer/APAudioPlayer.m @@ -14,6 +14,8 @@ @interface APAudioPlayer () { HSTREAM _channel; } +@property (assign, nonatomic) APAudioPlayerState currentState; + /* Represents current notification center */ @property (nonatomic, strong) NSNotificationCenter *notificationCenter; @@ -73,6 +75,8 @@ - (instancetype)initWithNotificationCenter:(NSNotificationCenter *)notificationC //Set volume _volume = BASS_GetConfig(BASS_CONFIG_GVOL_STREAM) / 10000.0f; + + _currentState = APAudioPlayerStateStopped; } return self; } @@ -83,56 +87,116 @@ - (void)dealloc BASS_Free(); } +- (void) setCurrentState:(APAudioPlayerState)currentState{ + + _currentState = currentState; + + __weak typeof(self) weakSelf = self; + dispatch_async(dispatch_get_main_queue(), ^{ + [weakSelf.player _notifyStatusChanged]; + }); +} + #pragma mark - #pragma mark - Public API #pragma mark - #pragma mark - Controls -- (BOOL)loadItemWithURL:(NSURL *)url autoPlay:(BOOL)autoplay +- (void)loadItemWithPath:(NSURL *)urlPath autoPlay:(BOOL)autoplay { - [self.audioSession setCategory:AVAudioSessionCategoryPlayback error: nil]; - [self.audioSession setActive:YES error:nil]; - //Stop channel; - BASS_ChannelStop(_channel); + self.currentState = APAudioPlayerStateBuffering; - //Free memory - BASS_StreamFree(_channel); + __weak typeof(self) weakSelf = self; + + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ + + [weakSelf.audioSession setCategory:AVAudioSessionCategoryPlayback error: nil]; + [weakSelf.audioSession setActive:YES error:nil]; + + //Stop channel; + BASS_ChannelStop(_channel); + + //Free memory + BASS_StreamFree(_channel); + + _channel = BASS_StreamCreateFile(FALSE, [[urlPath path] cStringUsingEncoding:NSUTF8StringEncoding], 0, 0, 0); + + //Set callback + BASS_ChannelSetSync(_channel, BASS_SYNC_END, 0, ChannelEndedCallback, (__bridge void *)weakSelf); + + /* Play if needed */ + if (autoplay) { + + dispatch_async(dispatch_get_main_queue(), ^{ + [weakSelf play]; + }); + } + + int code = BASS_ErrorGetCode(); + + }); +} + +- (void)loadItemWithURL:(NSURL *)url autoPlay:(BOOL)autoplay +{ - _channel = BASS_StreamCreateFile(FALSE, [[url path] cStringUsingEncoding:NSUTF8StringEncoding], 0, 0, 0); + self.currentState = APAudioPlayerStateBuffering; - //Set callback - BASS_ChannelSetSync(_channel, BASS_SYNC_END, 0, ChannelEndedCallback, (__bridge void *)self); + __weak typeof(self) weakSelf = self; - /* Play if needed */ - if (autoplay) { - [self play]; - } - - int code = BASS_ErrorGetCode(); - return code == 0; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ + + [weakSelf.audioSession setCategory:AVAudioSessionCategoryPlayback error: nil]; + [weakSelf.audioSession setActive:YES error:nil]; + + //Stop channel; + BASS_ChannelStop(_channel); + + //Free memory + BASS_StreamFree(_channel); + + + _channel = BASS_StreamCreateURL([[url absoluteString] cStringUsingEncoding:NSUTF8StringEncoding], 0, 0, NULL, NULL); + //_channel = BASS_StreamCreateFile(FALSE, [[url path] cStringUsingEncoding:NSUTF8StringEncoding], 0, 0, 0); + + //Set callback + BASS_ChannelSetSync(_channel, BASS_SYNC_END, 0, ChannelEndedCallback, (__bridge void *)weakSelf); + + /* Play if needed */ + if (autoplay) { + + dispatch_async(dispatch_get_main_queue(), ^{ + [weakSelf play]; + }); + } + + int code = BASS_ErrorGetCode(); + + }); } - (void)pause { BASS_ChannelPause(_channel); - [self _notifyStatusChanged]; + self.currentState = APAudioPlayerStatePaused; } - (void)play { - BASS_ChannelPlay(_channel, NO); - - [self _notifyStatusChanged]; + if (self.currentState != APAudioPlayerStateStopped) { + + BASS_ChannelPlay(_channel, NO); + self.currentState = APAudioPlayerStatePlaying; + } } - (void)stop { BASS_ChannelStop(_channel); - - [self _notifyStatusChanged]; + self.currentState = APAudioPlayerStateStopped; } - (BOOL)isPlaying