0

Je construis une application et utilise Spotify. J'ai réussi à démarrer et mettre en pause une chanson .... Mais je ne peux pas obtenir ma tête autour du bouton playnext et playprevious. peut-il s'il vous plaît dites-moi comment gérer ces événements avec le libspotify playbackmanager. J'ai une vue de liste de lecture qui passe courant à mon songviewController. Comment puis-je appeler de mon songViewController la chanson suivante. C'est comme ça que j'appelle le courant ...Comment jouer la prochaine piste du tableau

- (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]; 
         } 
      } 
     ]; 
    } 
} 

Répondre

0

CocoaLibSpotify est un peu bizarre de cette façon. Pour arrêter ou "mettre en pause" une chanson, il suffit de mettre le fichier "playbackManager.isPlaying" à "no". Une fois que vous avez arrêté la chanson en cours, vous pouvez appeler playTrack sur la piste suivante de votre playlist. Il pourrait aller quelque chose comme:

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]; 

     } 
    } 

Où l'index suivant gardera la trace de l'endroit où vous êtes dans la liste de lecture, par ex.

@property (assign) NSInteger nextIndex; 

Et le playist est une instance de SPPlaylist e.g.

@property (nonatomic) SPPlaylist *playlist; 

Ensuite, vous pouvez faire quelques initialisations sur ceux-ci dans viewDidLoad. Pour plus de commodité, j'embaquerais probablement cette méthode playTrack dans une autre méthode pour arrêter la duplication du code. Notez comment une propriété playlistItem.item est essentiellement 100% du temps un SPTrack

+0

Salut @GregPrice, merci pour la réponse. qu'est-ce que nextIndex ++ dois-je créer une propriété pour cela? –

+0

Oui, nextIndex ici représente juste quelque chose qui gardera la trace de l'endroit où vous êtes dans la playlist. Voir ci-dessus modifier –

+0

Cher @GregPrice j'ai essayé votre avis, mais je reçois une erreur en disant que la mauvaise instance est envoyée au sélecteur –

Questions connexes