【问题标题】:How to play next track from array如何从数组中播放下一首曲目
【发布时间】:2013-07-26 11:53:34
【问题描述】:

我正在构建一个应用程序并使用 spotify。我设法开始和暂停一首歌....但是我无法理解 playnext 按钮和 playprevious 按钮。有人可以告诉我如何使用 libspotify 播放管理器处理这些事件。我有一个播放列表视图,它将 currentsong 传递给我的 songviewController..我如何从我的 songViewController 中调用下一首歌曲。这就是我如何称呼当前歌曲...

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

【问题讨论】:

    标签: ios objective-c uitableview cocoalibspotify-2.0 libspotify


    【解决方案1】:

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

    Where next index 将跟踪您在播放列表中的位置,例如

    @property (assign) NSInteger nextIndex;
    

    播放列表是 SPPlaylist 的一个实例,例如

    @property (nonatomic) SPPlaylist *playlist;
    

    然后你可以在 viewDidLoad 中对这些做一些初始化。 为方便起见,我可能会将该 playTrack 方法包装在另一种方法中以停止代码重复。请注意 playlistItem.item 属性基本上 100% 的时间是 SPTrack

    【讨论】:

    • 嗨@GregPrice,感谢您的回复。 nextIndex++ 是什么,我需要为此创建一个属性吗?
    • 是的,这里的 nextIndex 只是表示可以跟踪您在播放列表中的位置的内容。见上面的编辑
    • 亲爱的@GregPrice 我尝试了你的建议,但我收到一个错误,说有错误的实例发送到选择器
    • 它说的是哪个对象?或者你能找到/发布错误发生的行吗?
    • '-[SPPlaylistItem 可用性]:无法识别的选择器发送到实例 0x95f66a0'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 2021-01-17
    • 2018-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多