Browse files

Publish when track ends

  • Loading branch information...
1 parent 5e06718 commit 8b793981fa0a692733acab135e7174bf9a4d1a42 @tonywok tonywok committed Mar 13, 2012
Showing with 46 additions and 15 deletions.
  1. +1 −0 cocoa_spotify/SpotboxPlayer.h
  2. +44 −9 cocoa_spotify/SpotboxPlayer.m
  3. +1 −4 cocoa_spotify/ZmqDispatch.h
  4. +0 −2 cocoa_spotify/ZmqDispatch.m
View
1 cocoa_spotify/SpotboxPlayer.h
@@ -15,6 +15,7 @@
}
@property(retain) SPPlaybackManager *playback_manager;
+@property(retain) ZmqDispatch *dispatcher;
- (void) play_track:(NSString *)track_url;
- (void) stop_track;
View
53 cocoa_spotify/SpotboxPlayer.m
@@ -10,17 +10,38 @@
@implementation SpotboxPlayer
-@synthesize playback_manager;
+@synthesize playback_manager, dispatcher;
- (id) init {
self = [super init];
if (self) {
SPSession *session = [SPSession sharedSession];
playback_manager = [[SPPlaybackManager alloc] initWithPlaybackSession:session];
+
+ [self addObserver:self
+ forKeyPath:@"playback_manager.currentTrack"
+ options:NSKeyValueObservingOptionNew
+ context:nil];
}
return self;
}
+// Observe currentTrack going to NULL, this signifies the end of a track
+//
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
+ if ([keyPath isEqualToString:@"playback_manager.currentTrack"]) {
+ if ([change valueForKey:@"new"] == [NSNull null]) {
+ NSLog(@"track ended");
+ NSString *message = [NSString stringWithFormat:@"%@::%@", @"spotbox:server", @"track_ended"];
+ NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding];
+ [dispatcher.pub sendData:data withFlags:ZMQ_NOBLOCK];
+ }
+ }
+}
+
+
+// Play a spotify track
+//
- (void) play_track:(NSString *)track_str {
NSURL *track_url = [[NSURL alloc] initWithString:track_str];
SPTrack *track = [[SPSession sharedSession] trackForURL:track_url];
@@ -42,6 +63,8 @@ - (void) play_track:(NSString *)track_str {
NSBeep();
}
+// Pause/Unpause track
+//
- (void) pause_track {
if ([playback_manager isPlaying]) {
[playback_manager setIsPlaying:NO];
@@ -52,25 +75,37 @@ - (void) pause_track {
}
}
+// Stop a track
+//
- (void) stop_track {
[playback_manager.playbackSession unloadPlayback];
}
-// ********** ZmqDispatch Delegate ******* //
-
-- (void) zmqDispatchDidReceiveData:(ZmqDispatch *)dispatcher {
+// Report track progress of currently playing track
+//
+- (void) report_track_progress {
if ([playback_manager isPlaying]) {
NSTimeInterval pos = [playback_manager trackPosition];
NSString *track_position = [[NSString alloc] initWithFormat:@"%d", (long)pos];
NSString *message = [NSString stringWithFormat:@"%@::%@::%@", @"spotbox:server", @"track_progress", track_position];
- NSData* data = [message dataUsingEncoding:NSUTF8StringEncoding];
-
+ NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding];
+
[dispatcher.pub sendData:data withFlags:ZMQ_NOBLOCK];
}
}
+// ********** ZmqDispatch Delegate ******* //
+
+// Called when zmq dispatcher receives data. Assigns self.dispatcher so this class can publish
+//
+- (void) zmqDispatchDidReceiveData:(ZmqDispatch *)aDispatcher {
+ // Set dispatcher upon successfully recieving data
+ if (!aDispatcher) { self.dispatcher = aDispatcher; }
+
+ [self report_track_progress];
+}
+
- (void) zmqDispatchDidReceivePlay:(NSString *)track_url {
- NSLog(@"PLAY");
[self play_track:track_url];
}
@@ -108,10 +143,10 @@ - (void)session:(SPSession *)aSession recievedMessageForUser:(NSString *)aMessag
NSLog(@"received msg for user: %@", aMessage);
}
-// Unregister observers
+// Remove observers
- (void) dealloc {
-
+ [self removeObserver:self forKeyPath:@"playback_manager.currentTrack"];
}
@end
View
5 cocoa_spotify/ZmqDispatch.h
@@ -14,11 +14,8 @@
@protocol ZmqDispatchDelegate <NSObject>
- (void) zmqDispatchDidReceivePlay:(NSString *)track_url;
-
-@optional
-- (void) zmqDispatchDidReceiveData:(ZmqDispatch *)dispatcher;
+- (void) zmqDispatchDidReceiveData:(ZmqDispatch *)aDispatcher;
- (void) zmqDispatchDidReceiveStop;
-- (void) zmqDispatchDidReceiveNext;
- (void) zmqDispatchDidReceivePause;
@end
View
2 cocoa_spotify/ZmqDispatch.m
@@ -61,8 +61,6 @@ - (void) receiveData:(NSTimer *)timer {
NSDictionary *parsedMessage = [self parseMessage:msg];
NSString *method_name = [parsedMessage valueForKey:@"method"];
- NSLog(@"message!!!!!! %@", parsedMessage);
-
if ([method_name isEqualToString:@"play"]) {
NSString *track_str = [[parsedMessage valueForKey:@"args"] objectAtIndex:0];
[delegate zmqDispatchDidReceivePlay:track_str];

0 comments on commit 8b79398

Please sign in to comment.