如何播放阵列中的下一首曲目



我正在构建一个应用程序并使用spotify。我设法开始并暂停了一首歌。。。。但我无法理解播放下一个按钮和播放上一个按钮。请告诉我如何使用libspotify playbackmanager处理这些事件。我有一个播放列表视图,它将当前歌曲传递到我的歌曲视图控制器。。如何从我的歌曲ViewController中调用下一首歌曲。这就是我对当前歌曲的称呼。。。

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

CocoaLibSpotify在这种方式上有点奇怪。要停止或"暂停"一首歌,只需将playbackManager.isPlaying设置为no。一旦停止了当前歌曲,就可以安全地调用播放列表中下一首曲目的playTrack。它可能会变成这样:

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

下一个索引将跟踪您在播放列表中的位置,例如

@property (assign) NSInteger nextIndex;

玩家是SPPlaylist的一个实例,例如

@property (nonatomic) SPPlaylist *playlist;

然后你可以在viewDidLoad中对它们进行一些初始化。为了方便起见,我可能会将playTrack方法封装在另一个方法中,以阻止代码重复。请注意,playlistItem.item属性在SPTrack

的100%时间内基本上都是

相关内容

  • 没有找到相关文章

最新更新