Permalink
Browse files

Player/Playlist converted to use notifications

  • Loading branch information...
1 parent 3bbc649 commit e5abf620b1d96c5f37b7b7d85ad522a6e65d802e @tonywok tonywok committed Apr 9, 2012
Showing with 17 additions and 15 deletions.
  1. +12 −10 cocoa_spotify/SpotboxPlayer.m
  2. +4 −4 cocoa_spotify/SpotboxPlaylist.m
  3. +1 −1 cocoa_spotify/ZmqDispatch.m
@@ -28,9 +28,10 @@ - (id) initWithDispatcher:(ZmqDispatch *)aDispatcher {
// Register for notifications
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reportTrackProgress:) name:@"reportTrackProgress" object:NULL];
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reportTrackProgress:) name:@"didReceieveData" object:NULL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playTrack:) name:@"play" object:NULL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pauseTrack:) name:@"pause" object:NULL];
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(unpauseTrack:) name:@"unpause" object:NULL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopTrack:) name:@"stop" object:NULL];
}
return self;
@@ -46,8 +47,7 @@ - (void) sendMessage:(NSString *)msg {
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"playbackManager.currentTrack"]) {
if ([change valueForKey:@"new"] == [NSNull null]) {
- NSLog(@"track ended");
- [self sendMessage:@"spotbox:server::track_ended"];
+ [self sendMessage:@"spotbox:server::trackEnded"];
}
}
}
@@ -77,19 +77,21 @@ - (void) pauseTrack:(NSNotification *) notification {
if ([playbackManager isPlaying]) {
[playbackManager setIsPlaying:NO];
[self sendMessage:@"spotbox:server::paused"];
- } else {
- NSTimeInterval trackPosition = [playbackManager trackPosition];
- [playbackManager seekToTrackPosition:trackPosition];
- [playbackManager setIsPlaying:YES];
- [self sendMessage:@"spotbox:server::unpaused"];
- }
+ }
+}
+
+- (void) unpauseTrack:(NSNotification *) notification {
+ NSTimeInterval trackPosition = [playbackManager trackPosition];
+ [playbackManager seekToTrackPosition:trackPosition];
+ [playbackManager setIsPlaying:YES];
+ [self sendMessage:@"spotbox:server::unpaused"];
}
- (void) reportTrackProgress:(NSNotification *) notification {
if ([playbackManager isPlaying]) {
NSTimeInterval pos = [playbackManager trackPosition];
NSString *trackPosition = [[NSString alloc] initWithFormat:@"%d", (long)pos];
- [self sendMessage:[NSString stringWithFormat:@"%@::%@", @"spotbox:server::track_progress", trackPosition]];
+ [self sendMessage:[NSString stringWithFormat:@"%@::%@", @"spotbox:server::trackProgress", trackPosition]];
}
}
@@ -17,7 +17,7 @@ - (id) initWithDispatcher:(ZmqDispatch *)aDispatcher {
if (self) {
self.dispatcher = aDispatcher;
[[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(load_playlist:)
+ selector:@selector(loadPlaylist:)
name:@"loadPlaylist"
object:nil];
}
@@ -38,8 +38,8 @@ - (void) reportPlaylistTracks:(SPPlaylist *)playlist {
NSString *playlistUrlStr = [[playlist spotifyURL] absoluteString];
NSString *playlistName = [playlist name];
NSString *tracksStr = [trackUrls componentsJoinedByString:@","];
- NSString *msg = [NSString stringWithFormat:@"spotbox:server::playlist_loaded::%@,%@,%@", playlistUrlStr, playlistName, tracksStr];
-
+ NSString *msg = [NSString stringWithFormat:@"spotbox:server::playlistLoaded::%@,%@,%@", playlistUrlStr, playlistName, tracksStr];
+
[[dispatcher pub] sendData:[msg dataUsingEncoding:NSUTF8StringEncoding] withFlags:ZMQ_NOBLOCK];
}
@@ -58,7 +58,7 @@ - (void) loadTracksFromPlaylist:(NSURL *)playlistUrl {
//**************** ZmqDispatch Notifications *********************//
-- (void) load_playlist:(NSNotification *)notification {
+- (void) loadPlaylist:(NSNotification *)notification {
NSString *playlistStr = [[[notification userInfo] valueForKey:@"args"] objectAtIndex:0];
NSURL *playlistUrl = [[NSURL alloc] initWithString:playlistStr];
@@ -54,7 +54,7 @@ - (NSDictionary *) parseMessage:(NSString *)zmqMessage {
- (void) receiveData:(NSTimer *)timer {
NSData *data = [sub receiveDataWithFlags:ZMQ_NOBLOCK];
- [[NSNotificationCenter defaultCenter] postNotificationName:@"didReceiveData" object:self];
+ [[NSNotificationCenter defaultCenter] postNotificationName:@"didReceieveData" object:self];
if (data) {
NSString *msg = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];

0 comments on commit e5abf62

Please sign in to comment.