Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audio Streaming and player state #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions APAudioPlayer/APAudioPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,37 @@

#import <Foundation/Foundation.h>

typedef NS_ENUM(NSInteger, APAudioPlayerState) {
APAudioPlayerStateStopped,
APAudioPlayerStatePlaying,
APAudioPlayerStatePaused,
APAudioPlayerStateError,
APAudioPlayerStateBuffering
};

@protocol APAudioPlayerDelegate;
@interface APAudioPlayer : NSObject

@property (nonatomic, weak) id <APAudioPlayerDelegate> 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
*
* @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
Expand Down
110 changes: 87 additions & 23 deletions APAudioPlayer/APAudioPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ @interface APAudioPlayer () <AVAudioSessionDelegate> {
HSTREAM _channel;
}

@property (assign, nonatomic) APAudioPlayerState currentState;

/* Represents current notification center */
@property (nonatomic, strong) NSNotificationCenter *notificationCenter;

Expand Down Expand Up @@ -73,6 +75,8 @@ - (instancetype)initWithNotificationCenter:(NSNotificationCenter *)notificationC

//Set volume
_volume = BASS_GetConfig(BASS_CONFIG_GVOL_STREAM) / 10000.0f;

_currentState = APAudioPlayerStateStopped;
}
return self;
}
Expand All @@ -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
Expand Down