当我选择 UITableViewCell 时播放音乐

发布于 2024-11-05 15:04:59 字数 969 浏览 5 评论 0 原文

我可以在 UITableView 上显示音乐库。

  songsArray = [[NSMutableArray alloc]init];
MPMediaQuery *playlistQuery = [[MPMediaQuery alloc]init];
[playlistQuery setGroupingType:MPMediaGroupingTitle];
songArray = [playlistQuery items];
for (MPMediaItem *song in songArray) {
    NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];        
    [songsArray addObject:songTitle];
}
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:                (NSIndexPath *)indexPath {
    cell.textLabel.text = [songsArray objectAtIndex:row];
[cell.textLabel setTextColor:[UIColor whiteColor]];

但是当我选择 UITableView Cell 时,我无法播放音乐。

    NSUInteger row = indexPath.row;

NSString *selectedSong = [songsArray objectAtIndex:row];
MPMusicPlayerController *appPlayer = [MPMusicPlayerController iPodMusicPlayer];

[appPlayer setQueueWithQuery:selectedSong];

[appPlayer play];

}

I can show music library on UITableView.

  songsArray = [[NSMutableArray alloc]init];
MPMediaQuery *playlistQuery = [[MPMediaQuery alloc]init];
[playlistQuery setGroupingType:MPMediaGroupingTitle];
songArray = [playlistQuery items];
for (MPMediaItem *song in songArray) {
    NSString *songTitle = [song valueForProperty: MPMediaItemPropertyTitle];        
    [songsArray addObject:songTitle];
}
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:                (NSIndexPath *)indexPath {
    cell.textLabel.text = [songsArray objectAtIndex:row];
[cell.textLabel setTextColor:[UIColor whiteColor]];

But I can't play music when I selected UITableView Cell.

    NSUInteger row = indexPath.row;

NSString *selectedSong = [songsArray objectAtIndex:row];
MPMusicPlayerController *appPlayer = [MPMusicPlayerController iPodMusicPlayer];

[appPlayer setQueueWithQuery:selectedSong];

[appPlayer play];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

策马西风 2024-11-12 15:04:59

啊,我看到了问题:

将此代码移动

NSUInteger row = indexPath.row;
NSString *selectedSong = [songsArray objectAtIndex:row];
MPMusicPlayerController *appPlayer = [MPMusicPlayerController iPodMusicPlayer];
[appPlayer setQueueWithQuery:selectedSong];
[appPlayer play];

到 UITableViewDelegate 协议方法“didSelectRowAtIndexPath”,如下所示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = indexPath.row;
    NSString *selectedSong = [songsArray objectAtIndex:row];
    MPMusicPlayerController *appPlayer = [MPMusicPlayerController iPodMusicPlayer];
    [appPlayer setQueueWithQuery:selectedSong];
    [appPlayer play];
}

编辑

文档:

Ah I see the problem:

Move this code:

NSUInteger row = indexPath.row;
NSString *selectedSong = [songsArray objectAtIndex:row];
MPMusicPlayerController *appPlayer = [MPMusicPlayerController iPodMusicPlayer];
[appPlayer setQueueWithQuery:selectedSong];
[appPlayer play];

to the UITableViewDelegate protocol method 'didSelectRowAtIndexPath' like this:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUInteger row = indexPath.row;
    NSString *selectedSong = [songsArray objectAtIndex:row];
    MPMusicPlayerController *appPlayer = [MPMusicPlayerController iPodMusicPlayer];
    [appPlayer setQueueWithQuery:selectedSong];
    [appPlayer play];
}

Edit

Documentation:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文