From f321f1a0d7641c30ba82e0a66e65bd21cbe6d371 Mon Sep 17 00:00:00 2001 From: Thong Nguyen Date: Fri, 31 Jan 2014 20:33:15 +0000 Subject: [PATCH] More code tidy-ups --- StreamingKit/StreamingKit/STKAudioPlayer.h | 10 +++++- StreamingKit/StreamingKit/STKAudioPlayer.m | 41 ++++++++++++++++------ 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/StreamingKit/StreamingKit/STKAudioPlayer.h b/StreamingKit/StreamingKit/STKAudioPlayer.h index 2c6e063..b7f1d49 100644 --- a/StreamingKit/StreamingKit/STKAudioPlayer.h +++ b/StreamingKit/StreamingKit/STKAudioPlayer.h @@ -95,6 +95,13 @@ typedef enum } STKAudioPlayerErrorCode; +typedef enum +{ + STKAudioPlayerOptionNone = 0, + STKAudioPlayerOptionDontFlushQueueOnSeek = 1 +} +STKAudioPlayerOptions; + @class STKAudioPlayer; @protocol STKAudioPlayerDelegate @@ -116,11 +123,12 @@ STKAudioPlayerErrorCode; @property (readonly) double duration; @property (readonly) double progress; @property (readwrite) STKAudioPlayerState state; +@property (readonly) STKAudioPlayerOptions options; @property (readonly) STKAudioPlayerStopReason stopReason; @property (readwrite, unsafe_unretained) id delegate; -(id) init; --(id) initWithReadBufferSize:(int)readBufferSizeIn; +-(id) initWithReadBufferSize:(int)readBufferSizeIn andOptions:(STKAudioPlayerOptions)options; +(STKDataSource*) dataSourceFromURL:(NSURL*)url; /// Plays an item from the given URL (all pending queued items are removed) diff --git a/StreamingKit/StreamingKit/STKAudioPlayer.m b/StreamingKit/StreamingKit/STKAudioPlayer.m index 210d5ef..b67bafc 100644 --- a/StreamingKit/StreamingKit/STKAudioPlayer.m +++ b/StreamingKit/StreamingKit/STKAudioPlayer.m @@ -40,7 +40,7 @@ #import "NSMutableArray+STKAudioPlayer.h" #import "libkern/OSAtomic.h" -#define STK_DEFAULT_PCM_BUFFER_SIZE_IN_SECONDS (65) +#define STK_DEFAULT_PCM_BUFFER_SIZE_IN_SECONDS (10) #define STK_DEFAULT_SECONDS_REQUIRED_TO_START_PLAYING (0) #define STK_MAX_COMPRESSED_PACKETS_FOR_BITRATE_CALCULATION (2048) @@ -57,6 +57,8 @@ UInt8* readBuffer; int readBufferSize; + STKAudioPlayerOptions options; + AudioComponentInstance audioUnit; UInt32 framesRequiredToStartPlaying; @@ -132,6 +134,12 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn @implementation STKAudioPlayer @synthesize delegate, internalState, state; + +-(STKAudioPlayerOptions) options +{ + return options; +} + -(STKAudioPlayerInternalState) internalState { return internalState; @@ -222,13 +230,15 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn -(id) init { - return [self initWithReadBufferSize:STK_DEFAULT_READ_BUFFER_SIZE]; + return [self initWithReadBufferSize:STK_DEFAULT_READ_BUFFER_SIZE andOptions:STKAudioPlayerOptionNone]; } --(id) initWithReadBufferSize:(int)readBufferSizeIn +-(id) initWithReadBufferSize:(int)readBufferSizeIn andOptions:(STKAudioPlayerOptions)optionsIn { if (self = [super init]) { + options = optionsIn; + canonicalAudioStreamBasicDescription.mSampleRate = 44100.00; canonicalAudioStreamBasicDescription.mFormatID = kAudioFormatLinearPCM; canonicalAudioStreamBasicDescription.mFormatFlags = kAudioFormatFlagsCanonical; @@ -1032,10 +1042,18 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn [currentlyReadingEntry.dataSource unregisterForEvents]; } - [self requeueBufferingEntries]; - - self.internalState = STKAudioPlayerInternalStateWaitingForDataAfterSeek; - [self setCurrentlyReadingEntry:currentlyPlayingEntry andStartPlaying:YES clearQueue:NO]; + if (self->options & STKAudioPlayerOptionDontFlushQueueOnSeek) + { + [self requeueBufferingEntries]; + + self.internalState = STKAudioPlayerInternalStateWaitingForDataAfterSeek; + [self setCurrentlyReadingEntry:currentlyPlayingEntry andStartPlaying:YES clearQueue:NO]; + } + else + { + self.internalState = STKAudioPlayerInternalStateWaitingForDataAfterSeek; + [self setCurrentlyReadingEntry:currentlyPlayingEntry andStartPlaying:YES clearQueue:YES]; + } } else if (currentlyReadingEntry == nil) { @@ -1531,7 +1549,7 @@ static void AudioFileStreamPacketsProc(void* clientData, UInt32 numberBytes, UIn #define kOutputBus 0 #define kInputBus 1 -BOOL GetHardwareCodecClassDesc(UInt32 formatId, AudioClassDescription* classDesc) +static BOOL GetHardwareCodecClassDesc(UInt32 formatId, AudioClassDescription* classDesc) { UInt32 size; @@ -2010,11 +2028,11 @@ static OSStatus playbackCallback(void* inRefCon, AudioUnitRenderActionFlags* ioA { if (state == STKAudioPlayerInternalStateWaitingForData) { - NSLog(@"Starting"); + // Starting } else if (state == STKAudioPlayerInternalStateRebuffering) { - NSLog(@"Buffering resuming"); + // Resuming from buffering } if (end > start) @@ -2071,7 +2089,8 @@ static OSStatus playbackCallback(void* inRefCon, AudioUnitRenderActionFlags* ioA if (!(entry == nil || state == STKAudioPlayerInternalStateWaitingForDataAfterSeek || state == STKAudioPlayerInternalStateWaitingForData || state == STKAudioPlayerInternalStateRebuffering)) { - NSLog(@"Buffering"); + // Buffering + audioPlayer.internalState = STKAudioPlayerInternalStateRebuffering; } }