Question

I'm building an app and using spotify. I managed to start and pause a song....But i can't get my head around the playnext button and playprevious button. can some please tell me how to handle these events with the libspotify playbackmanager. I have a playlist view which passes currentsong to my songviewController..How can i call from within my songViewController the next song. This is how i'm calling the currentsong...

 - (void)viewDidLoad
    {
        [super viewDidLoad];

        self.playbackManager = [[SPPlaybackManager alloc] initWithPlaybackSession:[SPSession sharedSession]];

        [self addObserver:self forKeyPath:@"currentSong.name" options:NSKeyValueObservingOptionInitial context:nil];
        [self addObserver:self forKeyPath:@"currentSong.artists" options:NSKeyValueObservingOptionInitial context:nil];
        [self addObserver:self forKeyPath:@"currentSong.album.cover.image" options:NSKeyValueObservingOptionInitial context:nil];

        [SPTrack trackForTrackURL:self.currentSong.spotifyURL inSession:[SPSession sharedSession] callback:^(SPTrack *track) {

            [self.playbackManager playTrack:track callback:^(NSError *error) {

                if (error) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot Play Track"
                                                                    message:[error localizedDescription]
                                                                   delegate:nil
                                                          cancelButtonTitle:@"OK"
                                                          otherButtonTitles:nil];
                    [alert show];

                }

            }];

        }];

    }

    - (void)viewDidUnload
    {
        [self trackArtist];
        [self trackTitle];
        [self coverView];
        [self currentSong];

        [self removeObserver:self forKeyPath:@"currentSong.name"];
        [self removeObserver:self forKeyPath:@"currentSong.artists"];
        [self removeObserver:self forKeyPath:@"currentSong.album.cover.image"];
        self.playbackManager.playbackSession.playing = NO;


        [super viewDidUnload];
        // Release any retained subviews of the main view.

    }




    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
        if ([keyPath isEqualToString:@"currentSong.name"]) {
            self.trackTitle.text = self.currentSong.name;
        } else if ([keyPath isEqualToString:@"currentSong.artists"]) {
            self.trackArtist.text = [[self.currentSong.artists valueForKey:@"name"] componentsJoinedByString:@","];
        } else if ([keyPath isEqualToString:@"currentSong.album.cover.image"]) {
            self.coverView.image = self.currentSong.album.cover.image;
        }  else {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
    }


    #pragma mark -
    - (IBAction)playTrack:(id)sender {
        if (self.playbackManager.isPlaying == NO) {
            self.playbackManager.isPlaying = YES;
        }

            else if (self.playbackManager.playbackSession.playing == YES) {
                self.playbackManager.isPlaying = NO;
            }

    }


  -(IBAction)nextTrack:(id)sender
{
    NSLog(@"index is : %i",nextIndex);
    NSLog(@"playlistItems: %i",[currentPlaylist.items count] );

    self.nextIndex++;
    NSLog(@"index is : %i",nextIndex);
    if (self.playbackManager.isPlaying == YES)
        self.playbackManager.isPlaying = NO;
    NSLog(@"0=yes 1=no : %i",_playbackManager.isPlaying);


    SPPlaylistItem *item = [self.currentPlaylist.items objectAtIndex:self.nextIndex];
    if (item.itemClass == [SPTrack class])
    {
        [self.playbackManager playTrack:(SPTrack*)item callback:^(NSError *error)
            {
                if (error)
                        {
                            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot Play Track"
                                                                message:[error localizedDescription]
                                                               delegate:nil
                                                      cancelButtonTitle:@"OK"
                                                      otherButtonTitles:nil];
                [alert show];
                        }
            }
         ];
    }
}
Was it helpful?

Solution

CocoaLibSpotify is a little weird in this way. To stop or "pause" a song you just set the playbackManager.isPlaying to no. Once you have stopped the current song it will be safe for you to call playTrack on the next track in your playlist. It could go something like:

 self.nextIndex++;
 if (self.playbackManager.isPlaying)
    self.playbackManager.isPlaying = NO;

 SPPlaylistItem *playlistItem = [self.playlist.items objectAtIndex:self.nextIndex];
 if (playlistItem.itemClass == [SPTrack class])
 {
     self.playbackManager playTrack:(SPtrack*)playlistItem.item callback:^(NSError *error) {

        if (error) 
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cannot Play Track"
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];

        }
  }

Where next index will keep track of where you are in the playlist e.g.

@property (assign) NSInteger nextIndex;

And the playist is an instance of SPPlaylist e.g.

@property (nonatomic) SPPlaylist *playlist;

Then you can do some initializations on these in viewDidLoad. For convenience I would probably wrap that playTrack method in another method to stop the code duplication. Notice how a playlistItem.item property is essentially 100% of the time an SPTrack

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top