Skip to content

Commit

Permalink
Added checks to stop UI updating, cleaned up code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Pannell committed Aug 24, 2010
1 parent 09adf1c commit 4fae118
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 35 deletions.
4 changes: 4 additions & 0 deletions Classes/iPhoneStreamingPlayerAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ @implementation iPhoneStreamingPlayerAppDelegate

@synthesize uiIsVisible;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
self.uiIsVisible = YES;
NSDictionary *credentialStorage =
[[NSURLCredentialStorage sharedCredentialStorage] allCredentials];
NSLog(@"Credentials: %@", credentialStorage);
Expand Down Expand Up @@ -89,6 +90,7 @@ - (void)applicationDidEnterBackground:(UIApplication *)application {
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/
self.uiIsVisible = NO;
[viewController createTimers:NO];
}


Expand All @@ -97,6 +99,8 @@ - (void)applicationWillEnterForeground:(UIApplication *)application {
Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
*/
self.uiIsVisible = YES;
[viewController createTimers:YES];
[viewController forceUIUpdate];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(presentAlertWithTitle:)
Expand Down
7 changes: 6 additions & 1 deletion Classes/iPhoneStreamingPlayerViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@
NSTimer *progressUpdateTimer;
NSTimer *levelMeterUpdateTimer;
LevelMeterView *levelMeterView;
NSString *currentArtist;
NSString *currentTitle;
}

- (IBAction)buttonPressed:(id)sender;
- (void)spinButton;
- (void)updateProgress:(NSTimer *)aNotification;
- (void)forceUIUpdate;
- (void)createTimers:(BOOL)create;
- (void)playbackStateChanged:(NSNotification *)aNotification;
- (void)updateProgress:(NSTimer *)updatedTimer;
- (IBAction)sliderMoved:(UISlider *)aSlider;

@end
Expand Down
117 changes: 83 additions & 34 deletions Classes/iPhoneStreamingPlayerViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ - (void)setButtonImage:(UIImage *)image
else
{
[button setImage:image forState:0];

if ([button.currentImage isEqual:[UIImage imageNamed:@"loadingbutton.png"]])
{
[self spinButton];
Expand All @@ -64,15 +64,64 @@ - (void)destroyStreamer
removeObserver:self
name:ASStatusChangedNotification
object:streamer];
[progressUpdateTimer invalidate];
progressUpdateTimer = nil;
[self createTimers:NO];

[streamer stop];
[streamer release];
streamer = nil;
}
}

//
// forceUIUpdate
//
// When foregrounded force UI update since we didn't update in the background
//
-(void)forceUIUpdate {
if (currentArtist)
metadataArtist.text = currentArtist;
if (currentTitle)
metadataTitle.text = currentTitle;

[self playbackStateChanged:NULL];
}

//
// createTimers
//
// Creates or destoys the timers
//
-(void)createTimers:(BOOL)create {
if (create) {
if (streamer) {
[self createTimers:NO];
progressUpdateTimer =
[NSTimer
scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(updateProgress:)
userInfo:nil
repeats:YES];
levelMeterUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:.1
target:self
selector:@selector(updateLevelMeters:)
userInfo:nil
repeats:YES];
}
}
else {
if (progressUpdateTimer)
{
[progressUpdateTimer invalidate];
progressUpdateTimer = nil;
}
if(levelMeterUpdateTimer) {
[levelMeterUpdateTimer invalidate];
levelMeterUpdateTimer = nil;
}
}
}

//
// createStreamer
//
Expand All @@ -99,18 +148,8 @@ - (void)createStreamer
NSURL *url = [NSURL URLWithString:escapedValue];
streamer = [[AudioStreamer alloc] initWithURL:url];

progressUpdateTimer =
[NSTimer
scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(updateProgress:)
userInfo:nil
repeats:YES];
levelMeterUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:.1
target:self
selector:@selector(updateLevelMeters:)
userInfo:nil
repeats:YES];
[self createTimers:YES];

[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(playbackStateChanged:)
Expand Down Expand Up @@ -225,7 +264,7 @@ - (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)finished
//
- (IBAction)buttonPressed:(id)sender
{
if ([button.currentImage isEqual:[UIImage imageNamed:@"playbutton.png"]])
if ([button.currentImage isEqual:[UIImage imageNamed:@"playbutton.png"]] || [button.currentImage isEqual:[UIImage imageNamed:@"pausebutton.png"]])
{
[downloadSourceField resignFirstResponder];

Expand Down Expand Up @@ -264,24 +303,40 @@ - (IBAction)sliderMoved:(UISlider *)aSlider
//
- (void)playbackStateChanged:(NSNotification *)aNotification
{
iPhoneStreamingPlayerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

if ([streamer isWaiting])
{
[levelMeterView updateMeterWithLeftValue:0.0
rightValue:0.0];
[streamer setMeteringEnabled:NO];
[self setButtonImage:[UIImage imageNamed:@"loadingbutton.png"]];
if (appDelegate.uiIsVisible) {
[levelMeterView updateMeterWithLeftValue:0.0
rightValue:0.0];
[streamer setMeteringEnabled:NO];
[self setButtonImage:[UIImage imageNamed:@"loadingbutton.png"]];
}
}
else if ([streamer isPlaying])
{
[streamer setMeteringEnabled:YES];
[self setButtonImage:[UIImage imageNamed:@"stopbutton.png"]];
if (appDelegate.uiIsVisible) {
[streamer setMeteringEnabled:YES];
[self setButtonImage:[UIImage imageNamed:@"stopbutton.png"]];
}
}
else if ([streamer isPaused]) {
if (appDelegate.uiIsVisible) {
[levelMeterView updateMeterWithLeftValue:0.0
rightValue:0.0];
[streamer setMeteringEnabled:NO];
[self setButtonImage:[UIImage imageNamed:@"pausebutton.png"]];
}
}
else if ([streamer isIdle])
{
[levelMeterView updateMeterWithLeftValue:0.0
rightValue:0.0];
if (appDelegate.uiIsVisible) {
[levelMeterView updateMeterWithLeftValue:0.0
rightValue:0.0];
[self setButtonImage:[UIImage imageNamed:@"playbutton.png"]];
}
[self destroyStreamer];
[self setButtonImage:[UIImage imageNamed:@"playbutton.png"]];
}
}

Expand Down Expand Up @@ -331,6 +386,8 @@ - (void)metadataChanged:(NSNotification *)aNotification
metadataArtist.text = streamArtist;
metadataTitle.text = streamTitle;
}
currentArtist = streamArtist;
currentTitle = streamTitle;
}
#endif

Expand Down Expand Up @@ -406,15 +463,7 @@ - (BOOL)textFieldShouldReturn:(UITextField *)sender
- (void)dealloc
{
[self destroyStreamer];
if (progressUpdateTimer)
{
[progressUpdateTimer invalidate];
progressUpdateTimer = nil;
}
if(levelMeterUpdateTimer) {
[levelMeterUpdateTimer invalidate];
levelMeterUpdateTimer = nil;
}
[self createTimers:NO];
[levelMeterView release];
[super dealloc];
}
Expand Down
16 changes: 16 additions & 0 deletions iPhoneStreamingPlayer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
C9E6730A0FE8C5650033BF43 /* stopbutton.png in Resources */ = {isa = PBXBuildFile; fileRef = C9E673060FE8C5650033BF43 /* stopbutton.png */; };
C9E6730B0FE8C5650033BF43 /* loadingbutton.png in Resources */ = {isa = PBXBuildFile; fileRef = C9E673070FE8C5650033BF43 /* loadingbutton.png */; };
C9E6730C0FE8C5650033BF43 /* pausebutton.png in Resources */ = {isa = PBXBuildFile; fileRef = C9E673080FE8C5650033BF43 /* pausebutton.png */; };
FCB039021224369600E538F7 /* loadingbutton@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = FCB038FE1224369600E538F7 /* loadingbutton@2x.png */; };
FCB039031224369600E538F7 /* pausebutton@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = FCB038FF1224369600E538F7 /* pausebutton@2x.png */; };
FCB039041224369600E538F7 /* playbutton@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = FCB039001224369600E538F7 /* playbutton@2x.png */; };
FCB039051224369600E538F7 /* stopbutton@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = FCB039011224369600E538F7 /* stopbutton@2x.png */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -56,6 +60,10 @@
C9E673070FE8C5650033BF43 /* loadingbutton.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = loadingbutton.png; path = "Shared Resources/loadingbutton.png"; sourceTree = "<group>"; };
C9E673080FE8C5650033BF43 /* pausebutton.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = pausebutton.png; path = "Shared Resources/pausebutton.png"; sourceTree = "<group>"; };
C9E673410FE8C6510033BF43 /* iPhoneInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = iPhoneInfo.plist; sourceTree = "<group>"; };
FCB038FE1224369600E538F7 /* loadingbutton@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "loadingbutton@2x.png"; path = "Shared Resources/loadingbutton@2x.png"; sourceTree = "<group>"; };
FCB038FF1224369600E538F7 /* pausebutton@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "pausebutton@2x.png"; path = "Shared Resources/pausebutton@2x.png"; sourceTree = "<group>"; };
FCB039001224369600E538F7 /* playbutton@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "playbutton@2x.png"; path = "Shared Resources/playbutton@2x.png"; sourceTree = "<group>"; };
FCB039011224369600E538F7 /* stopbutton@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = "stopbutton@2x.png"; path = "Shared Resources/stopbutton@2x.png"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -125,6 +133,10 @@
29B97317FDCFA39411CA2CEA /* Resources */ = {
isa = PBXGroup;
children = (
FCB038FE1224369600E538F7 /* loadingbutton@2x.png */,
FCB038FF1224369600E538F7 /* pausebutton@2x.png */,
FCB039001224369600E538F7 /* playbutton@2x.png */,
FCB039011224369600E538F7 /* stopbutton@2x.png */,
C9E672FF0FE8C55B0033BF43 /* iPhoneStreamingPlayerViewController.xib */,
C9E673010FE8C55B0033BF43 /* MainWindow.xib */,
C9E673050FE8C5650033BF43 /* playbutton.png */,
Expand Down Expand Up @@ -206,6 +218,10 @@
C9E6730A0FE8C5650033BF43 /* stopbutton.png in Resources */,
C9E6730B0FE8C5650033BF43 /* loadingbutton.png in Resources */,
C9E6730C0FE8C5650033BF43 /* pausebutton.png in Resources */,
FCB039021224369600E538F7 /* loadingbutton@2x.png in Resources */,
FCB039031224369600E538F7 /* pausebutton@2x.png in Resources */,
FCB039041224369600E538F7 /* playbutton@2x.png in Resources */,
FCB039051224369600E538F7 /* stopbutton@2x.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit 4fae118

Please sign in to comment.