Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Publish when track ends

  • Loading branch information...
commit 8b793981fa0a692733acab135e7174bf9a4d1a42 1 parent 5e06718
Tony Schneider tonywok authored
1  cocoa_spotify/SpotboxPlayer.h
@@ -15,6 +15,7 @@
15 15 }
16 16
17 17 @property(retain) SPPlaybackManager *playback_manager;
  18 +@property(retain) ZmqDispatch *dispatcher;
18 19
19 20 - (void) play_track:(NSString *)track_url;
20 21 - (void) stop_track;
53 cocoa_spotify/SpotboxPlayer.m
@@ -10,17 +10,38 @@
10 10
11 11 @implementation SpotboxPlayer
12 12
13   -@synthesize playback_manager;
  13 +@synthesize playback_manager, dispatcher;
14 14
15 15 - (id) init {
16 16 self = [super init];
17 17 if (self) {
18 18 SPSession *session = [SPSession sharedSession];
19 19 playback_manager = [[SPPlaybackManager alloc] initWithPlaybackSession:session];
  20 +
  21 + [self addObserver:self
  22 + forKeyPath:@"playback_manager.currentTrack"
  23 + options:NSKeyValueObservingOptionNew
  24 + context:nil];
20 25 }
21 26 return self;
22 27 }
23 28
  29 +// Observe currentTrack going to NULL, this signifies the end of a track
  30 +//
  31 +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  32 + if ([keyPath isEqualToString:@"playback_manager.currentTrack"]) {
  33 + if ([change valueForKey:@"new"] == [NSNull null]) {
  34 + NSLog(@"track ended");
  35 + NSString *message = [NSString stringWithFormat:@"%@::%@", @"spotbox:server", @"track_ended"];
  36 + NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding];
  37 + [dispatcher.pub sendData:data withFlags:ZMQ_NOBLOCK];
  38 + }
  39 + }
  40 +}
  41 +
  42 +
  43 +// Play a spotify track
  44 +//
24 45 - (void) play_track:(NSString *)track_str {
25 46 NSURL *track_url = [[NSURL alloc] initWithString:track_str];
26 47 SPTrack *track = [[SPSession sharedSession] trackForURL:track_url];
@@ -42,6 +63,8 @@ - (void) play_track:(NSString *)track_str {
42 63 NSBeep();
43 64 }
44 65
  66 +// Pause/Unpause track
  67 +//
45 68 - (void) pause_track {
46 69 if ([playback_manager isPlaying]) {
47 70 [playback_manager setIsPlaying:NO];
@@ -52,25 +75,37 @@ - (void) pause_track {
52 75 }
53 76 }
54 77
  78 +// Stop a track
  79 +//
55 80 - (void) stop_track {
56 81 [playback_manager.playbackSession unloadPlayback];
57 82 }
58 83
59   -// ********** ZmqDispatch Delegate ******* //
60   -
61   -- (void) zmqDispatchDidReceiveData:(ZmqDispatch *)dispatcher {
  84 +// Report track progress of currently playing track
  85 +//
  86 +- (void) report_track_progress {
62 87 if ([playback_manager isPlaying]) {
63 88 NSTimeInterval pos = [playback_manager trackPosition];
64 89 NSString *track_position = [[NSString alloc] initWithFormat:@"%d", (long)pos];
65 90 NSString *message = [NSString stringWithFormat:@"%@::%@::%@", @"spotbox:server", @"track_progress", track_position];
66   - NSData* data = [message dataUsingEncoding:NSUTF8StringEncoding];
67   -
  91 + NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding];
  92 +
68 93 [dispatcher.pub sendData:data withFlags:ZMQ_NOBLOCK];
69 94 }
70 95 }
71 96
  97 +// ********** ZmqDispatch Delegate ******* //
  98 +
  99 +// Called when zmq dispatcher receives data. Assigns self.dispatcher so this class can publish
  100 +//
  101 +- (void) zmqDispatchDidReceiveData:(ZmqDispatch *)aDispatcher {
  102 + // Set dispatcher upon successfully recieving data
  103 + if (!aDispatcher) { self.dispatcher = aDispatcher; }
  104 +
  105 + [self report_track_progress];
  106 +}
  107 +
72 108 - (void) zmqDispatchDidReceivePlay:(NSString *)track_url {
73   - NSLog(@"PLAY");
74 109 [self play_track:track_url];
75 110 }
76 111
@@ -108,10 +143,10 @@ - (void)session:(SPSession *)aSession recievedMessageForUser:(NSString *)aMessag
108 143 NSLog(@"received msg for user: %@", aMessage);
109 144 }
110 145
111   -// Unregister observers
  146 +// Remove observers
112 147
113 148 - (void) dealloc {
114   -
  149 + [self removeObserver:self forKeyPath:@"playback_manager.currentTrack"];
115 150 }
116 151
117 152 @end
5 cocoa_spotify/ZmqDispatch.h
@@ -14,11 +14,8 @@
14 14 @protocol ZmqDispatchDelegate <NSObject>
15 15
16 16 - (void) zmqDispatchDidReceivePlay:(NSString *)track_url;
17   -
18   -@optional
19   -- (void) zmqDispatchDidReceiveData:(ZmqDispatch *)dispatcher;
  17 +- (void) zmqDispatchDidReceiveData:(ZmqDispatch *)aDispatcher;
20 18 - (void) zmqDispatchDidReceiveStop;
21   -- (void) zmqDispatchDidReceiveNext;
22 19 - (void) zmqDispatchDidReceivePause;
23 20
24 21 @end
2  cocoa_spotify/ZmqDispatch.m
@@ -61,8 +61,6 @@ - (void) receiveData:(NSTimer *)timer {
61 61 NSDictionary *parsedMessage = [self parseMessage:msg];
62 62 NSString *method_name = [parsedMessage valueForKey:@"method"];
63 63
64   - NSLog(@"message!!!!!! %@", parsedMessage);
65   -
66 64 if ([method_name isEqualToString:@"play"]) {
67 65 NSString *track_str = [[parsedMessage valueForKey:@"args"] objectAtIndex:0];
68 66 [delegate zmqDispatchDidReceivePlay:track_str];

0 comments on commit 8b79398

Please sign in to comment.
Something went wrong with that request. Please try again.