From d2a384947ca515dfbdc114c326e7ac4667c0397e Mon Sep 17 00:00:00 2001 From: Kent Sutherland Date: Tue, 8 Nov 2005 20:46:18 +0000 Subject: [PATCH] Added a currentSongPlayed method that returns the integer seconds played in the current track. More changes to audioscrobbler support. --- AudioscrobblerController.m | 9 ++++++--- ITMTRemote.h | 7 ++++++- ITMTRemote.m | 5 +++++ MainController.m | 33 +++++++++++++++++++++------------ PreferencesController.m | 1 + iTunesRemote.m | 9 +++++++++ 6 files changed, 48 insertions(+), 16 deletions(-) diff --git a/AudioscrobblerController.m b/AudioscrobblerController.m index 4addcaf..79bad7b 100644 --- a/AudioscrobblerController.m +++ b/AudioscrobblerController.m @@ -16,6 +16,9 @@ #import #import +#define AUDIOSCROBBLER_ID @"tst" +#define AUDIOSCROBBLER_VERSION [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] + static AudioscrobblerController *_sharedController = nil; @implementation AudioscrobblerController @@ -75,9 +78,9 @@ static AudioscrobblerController *_sharedController = nil; return; } - if (!_handshakeCompleted) { - NSString *version = [[[NSBundle bundleWithPath:[[NSWorkspace sharedWorkspace] fullPathForApplication:@"iTunes.app"]] infoDictionary] objectForKey:@"CFBundleVersion"], *user = @"Tristrex"; - NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://post.audioscrobbler.com/?hs=true&p=1.1&c=tst&v=%@&u=%@", version, user]]; + NSString *user = [[NSUserDefaults standardUserDefaults] stringForKey:@"audioscrobblerUser"]; + if (!_handshakeCompleted && user) { + NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://post.audioscrobbler.com/?hs=true&p=1.1&c=%@&v=%@&u=%@", AUDIOSCROBBLER_ID, AUDIOSCROBBLER_VERSION, user]]; _currentStatus = AudioscrobblerRequestingHandshakeStatus; _responseData = [[NSMutableData alloc] init]; diff --git a/ITMTRemote.h b/ITMTRemote.h index f49b559..1f84703 100755 --- a/ITMTRemote.h +++ b/ITMTRemote.h @@ -300,7 +300,12 @@ er's process managment. - (NSString *)currentSongLength; /*! - * @method currentSongDuratioh + * @method currentSongPlayed + */ +- (int)currentSongPlayed; + +/*! + * @method currentSongDuration */ - (int)currentSongDuration; diff --git a/ITMTRemote.m b/ITMTRemote.m index e4ff611..8431051 100755 --- a/ITMTRemote.m +++ b/ITMTRemote.m @@ -179,6 +179,11 @@ return nil; } +- (int)currentSongPlayed +{ + return -1; +} + - (int)currentSongDuration { return -1; diff --git a/MainController.m b/MainController.m index db0269f..d51b72f 100755 --- a/MainController.m +++ b/MainController.m @@ -469,8 +469,8 @@ static MainController *sharedController; if (_audioscrobblerTimer) { [_audioscrobblerTimer invalidate]; } - if (length > 0) { - _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:((length < 240) ? length / 2 : 120) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:NO]; + if (length > 30) { + _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:((length < 240) ? length / 2 : 120) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:YES]; } } else { _audioscrobblerTimer = nil; @@ -595,8 +595,8 @@ static MainController *sharedController; if (_audioscrobblerTimer) { [_audioscrobblerTimer invalidate]; } - if (length > 0) { - _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:((length < 240) ? length / 2 : 120) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:NO]; + if (length > 30) { + _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:((length < 240) ? length / 2 : 120) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:YES]; } } else { _audioscrobblerTimer = nil; @@ -614,20 +614,29 @@ static MainController *sharedController; - (void)submitAudioscrobblerTrack:(NSTimer *)timer { + int interval = [timer timeInterval]; + [timer invalidate]; + _audioscrobblerTimer = nil; + ITDebugLog(@"Audioscrobbler: Attempting to submit current track"); if ([df boolForKey:@"audioscrobblerEnabled"]) { NS_DURING - NSString *title = [[self currentRemote] currentSongTitle], *artist = [[self currentRemote] currentSongArtist]; - if (title && artist) { - [[AudioscrobblerController sharedController] submitTrack:title - artist:artist - album:[[self currentRemote] currentSongAlbum] - length:[[self currentRemote] currentSongDuration]]; + int elapsed = [[self currentRemote] currentSongPlayed]; + if ((abs(elapsed - interval) < 5) && ([[self currentRemote] playerPlayingState] == ITMTRemotePlayerPlaying)) { + NSString *title = [[self currentRemote] currentSongTitle], *artist = [[self currentRemote] currentSongArtist]; + if (title && artist) { + ITDebugLog(@"Audioscrobbler: Submitting current track"); + [[AudioscrobblerController sharedController] submitTrack:title + artist:artist + album:[[self currentRemote] currentSongAlbum] + length:[[self currentRemote] currentSongDuration]]; + } + } else if (interval - elapsed > 0) { + ITDebugLog(@"Audioscrobbler: Creating a new timer that will run in %i seconds", interval - elapsed); + _audioscrobblerTimer = [NSTimer scheduledTimerWithTimeInterval:(interval - elapsed) target:self selector:@selector(submitAudioscrobblerTrack:) userInfo:nil repeats:YES]; } NS_HANDLER [self networkError:localException]; NS_ENDHANDLER - [timer invalidate]; - _audioscrobblerTimer = nil; } } diff --git a/PreferencesController.m b/PreferencesController.m index f8245d2..61e0c11 100755 --- a/PreferencesController.m +++ b/PreferencesController.m @@ -1105,6 +1105,7 @@ static PreferencesController *prefs = nil; [audioscrobblerPasswordTextField setStringValue:password]; } } + [audioscrobblerUseCacheCheckbox setState:[df boolForKey:@"audioscrobblerCacheSubmissions"]]; [[NSNotificationCenter defaultCenter] addObserver:sharingTableView selector:@selector(reloadData) name:@"ITMTFoundNetService" object:nil]; diff --git a/iTunesRemote.m b/iTunesRemote.m index 11ba443..dbd21fb 100755 --- a/iTunesRemote.m +++ b/iTunesRemote.m @@ -670,6 +670,15 @@ return temp2; } +- (int)currentSongPlayed +{ + UInt32 final; + ITDebugLog(@"Getting current song played time."); + final = [ITSendAEWithKey('pPos', 'core', 'getd', &savedPSN) int32Value]; + ITDebugLog(@"Getting current song played time done."); + return final; +} + - (int)currentSongDuration { SInt32 temp1; -- 2.20.1