【问题标题】:Playing song with Apple Music API does not update control center or lock screen使用 Apple Music API 播放歌曲不会更新控制中心或锁定屏幕
【发布时间】:2018-11-06 06:33:07
【问题描述】:

我正在使用 Apple 的新 Apple Music API 来制作应用程序。我有一个带有歌曲列表的屏幕,用户可以点击一个来播放它。我的代码部分有效。轻按的歌曲将播放,但不会显示在控制中心。控制中心空无一物,好像什么都没有播放。

- (void)viewDidLoad {
    [super viewDidLoad];

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];


    [[[MPRemoteCommandCenter sharedCommandCenter] playCommand] addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        NSLog(@"play");
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    [[[MPRemoteCommandCenter sharedCommandCenter] pauseCommand] addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        NSLog(@"pause");
        return MPRemoteCommandHandlerStatusSuccess;

    }];

    [[[MPRemoteCommandCenter sharedCommandCenter] stopCommand] addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        return MPRemoteCommandHandlerStatusSuccess;

    }];

    [[[MPRemoteCommandCenter sharedCommandCenter] togglePlayPauseCommand] addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
        return MPRemoteCommandHandlerStatusSuccess;

    }];

    [[MPRemoteCommandCenter sharedCommandCenter] playCommand].enabled = YES;
    [[MPRemoteCommandCenter sharedCommandCenter] pauseCommand].enabled = YES;
    [[MPRemoteCommandCenter sharedCommandCenter] stopCommand].enabled = YES;
    [[MPRemoteCommandCenter sharedCommandCenter] togglePlayPauseCommand].enabled = YES;

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
    [audioSession setActive:YES error:nil];
}

```

点击单元格时:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
MPMusicPlayerApplicationController *controller = [MPMusicPlayerController applicationQueuePlayer];
[controller performQueueTransaction:^(MPMusicPlayerControllerMutableQueue * _Nonnull queue) {

    MPMusicPlayerStoreQueueDescriptor *queueDescripter = [[MPMusicPlayerStoreQueueDescriptor alloc] initWithStoreIDs:@[_album.songs[indexPath.row].identifier]];
    [queue insertQueueDescriptor:queueDescripter afterItem:nil];
} completionHandler:^(MPMusicPlayerControllerQueue * _Nonnull queue, NSError * _Nullable error) {
    [controller setNowPlayingItem:queue.items[0]];
    [controller prepareToPlay];
    [controller play];

    MPMediaItem *item = [controller nowPlayingItem];

    NSDictionary *info = @{MPMediaItemPropertyAlbumTitle:item.albumTitle, MPMediaItemPropertyArtist:album.artistName, MPMediaItemPropertyTitle:item.title, MPMediaItemPropertyArtwork:item.artwork, MPMediaItemPropertyPlaybackDuration:@(item.playbackDuration), MPNowPlayingInfoPropertyElapsedPlaybackTime:@0};
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:info];
}];

}

此外,此消息会记录到控制台。我不确定这是否与这个问题有关。 [RemoteControl] Error setting active player: (null)

【问题讨论】:

  • Apple Music 是一个单独的应用程序,似乎使用独占播放类别,所以我认为它与任何需要独占音频会话的东西不兼容,例如MPRemoteCommandCenter 命令。

标签: ios objective-c iphone apple-music


【解决方案1】:

尝试使用Apple's sample code,只要使用 applicationQueuePlayer,我就会观察到相同的行为。使用 systemMusicPlayer 使代码以您期望的方式工作,更新控制中心。

【讨论】:

  • 当然,但我的整个应用程序都以使用 applicationQueuePlayer 为中心,它需要这样做。所以改变它根本不是答案。
  • 嗨@Benr783 我遇到了类似的问题,正在使用 applicationQueuePlayer 你有没有想过这个问题?
  • 对于到达这个地方的人来说,这实际上对我有用;虽然我仍在尝试获得有关控制中心操作的反馈
【解决方案2】:

这是国际海事组织的一个错误。

rdar://33923587

好奇编辑:

我能够通过创建自己的音频会话而不使用混合选项来覆盖 Control Center/Lock 屏幕轨道描述(因此它暂停了音频 from Apple Music/Music Library)但这不是解决方案。

【讨论】:

  • 你试过iOS 11.2吗?它说“• 解决了可能阻止音乐控件显示在锁定屏幕上的问题”support.apple.com/en-us/HT208067
  • 不,我已经放弃了这个项目一段时间,但我会回到它:)
【解决方案3】:

MPMusicPlayerApplicationController 代表您与 MPRemoteCommandCenter 通信。为了查看控件,您需要做的就是设置一个包含多首歌曲的队列。

顺便说一句,在didSelectRowAtIndexPath: 中实例化MPMusicPlayerApplicationController 不是一个好习惯,因为每次用户点击单元格时都会实例化一个新的MPMusicPlayerApplicationController。您只需要一个应该在viewDidLoad: 中实例化、存储到 iVar 并根据需要进行配置的实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多