From 257532c6c95d510bb02c6fb1e3647f9240842ded Mon Sep 17 00:00:00 2001 From: Thong Nguyen Date: Thu, 23 Jan 2014 12:43:22 +0000 Subject: [PATCH] Added progress text to example app. Fixed finishedEvent not always being raised when user seeks to the last byte of the track --- ExampleApp/ExampleApp/AudioPlayerView.h | 1 + ExampleApp/ExampleApp/AudioPlayerView.m | 18 ++++++++++++++++++ StreamingKit/StreamingKit/STKAudioPlayer.m | 21 +++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/ExampleApp/ExampleApp/AudioPlayerView.h b/ExampleApp/ExampleApp/AudioPlayerView.h index c2f015a..41cf7c2 100644 --- a/ExampleApp/ExampleApp/AudioPlayerView.h +++ b/ExampleApp/ExampleApp/AudioPlayerView.h @@ -47,6 +47,7 @@ { @private NSTimer* timer; + UILabel* label; UISlider* slider; UISwitch* repeatSwitch; UIButton* playButton; diff --git a/ExampleApp/ExampleApp/AudioPlayerView.m b/ExampleApp/ExampleApp/AudioPlayerView.m index 9628b7e..f8aa2c4 100644 --- a/ExampleApp/ExampleApp/AudioPlayerView.m +++ b/ExampleApp/ExampleApp/AudioPlayerView.m @@ -81,13 +81,18 @@ size = CGSizeMake(80, 50); repeatSwitch = [[UISwitch alloc] initWithFrame:CGRectMake((320 - size.width) / 2, frame.size.height * 0.15 + 170, size.width, size.height)]; + + label = [[UILabel alloc] initWithFrame:CGRectMake(0, slider.frame.origin.y + slider.frame.size.height, frame.size.width, 50)]; + label.textAlignment = NSTextAlignmentCenter; + [self addSubview:slider]; [self addSubview:playButton]; [self addSubview:playFromHTTPButton]; [self addSubview:playFromLocalFileButton]; [self addSubview:queueShortFileButton]; [self addSubview:repeatSwitch]; + [self addSubview:label]; [self setupTimer]; [self updateControls]; @@ -120,6 +125,7 @@ if (!audioPlayer || audioPlayer.duration == 0) { slider.value = 0; + label.text = @""; return; } @@ -128,6 +134,8 @@ slider.maximumValue = audioPlayer.duration; slider.value = audioPlayer.progress; + + label.text = [NSString stringWithFormat:@"%@ - %@", [self formatTimeFromSeconds:audioPlayer.progress], [self formatTimeFromSeconds:audioPlayer.duration]]; } -(void) playFromHTTPButtonTouched @@ -162,6 +170,16 @@ } } +-(NSString*) formatTimeFromSeconds:(int)totalSeconds +{ + + int seconds = totalSeconds % 60; + int minutes = (totalSeconds / 60) % 60; + int hours = totalSeconds / 3600; + + return [NSString stringWithFormat:@"%02d:%02d:%02d", hours, minutes, seconds]; +} + -(void) updateControls { if (audioPlayer == nil) diff --git a/StreamingKit/StreamingKit/STKAudioPlayer.m b/StreamingKit/StreamingKit/STKAudioPlayer.m index 6e2c090..d259e7b 100644 --- a/StreamingKit/StreamingKit/STKAudioPlayer.m +++ b/StreamingKit/StreamingKit/STKAudioPlayer.m @@ -1101,6 +1101,8 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ } else if (entry.lastByteIndex == audioPacketsPlayedCount && entry.lastByteIndex != -1) { + [self logInfo:@"handleAudioQueueOutput:lastByteIndex"]; + BOOL everythingInBufferingQueueBuffered = YES; for (int i = 0; i < bufferingQueue.count; i++) @@ -2063,6 +2065,11 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ [currentEntry updateAudioDataSource]; [currentEntry.dataSource seekToOffset:seekByteOffset]; + if (self.internalState == AudioPlayerInternalStateFlushingAndStoppingButStillPlaying) + { + self.internalState = AudioPlayerInternalStatePlaying; + } + if (seekByteOffset > 0) { discontinuous = YES; @@ -2328,6 +2335,20 @@ static void AudioQueueIsRunningCallbackProc(void* userData, AudioQueueRef audioQ currentlyReadingEntry.lastFrameIndex = self->framesQueued; currentlyReadingEntry.lastByteIndex = audioPacketsReadCount; + [self logInfo:[NSString stringWithFormat:@"dataSourceEofLastByteIndex: %lld, %lld", audioPacketsReadCount, audioPacketsPlayedCount]]; + + if (audioPacketsReadCount == 0 && audioPacketsPlayedCount == audioPacketsReadCount) + { + [self logInfo:@"dataSourceEof shutting down audio queue"]; + + if (audioQueue) + { + self.internalState = AudioPlayerInternalStateFlushingAndStoppingButStillPlaying; + + AudioQueueStop(audioQueue, NO); + } + } + currentlyReadingEntry = nil; } else