gpt4 book ai didi

ios - 使用Apple Music API播放歌曲不会更新控制中心或锁定屏幕

转载 作者:行者123 更新时间:2023-12-01 19:53:05 27 4
gpt4 key购买 nike

我正在使用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's sample code,只要使用applicationQueuePlayer,我就会观察到相同的行为。使用systemMusicPlayer可使代码按您期望的方式工作,从而更新Control Center。

关于ios - 使用Apple Music API播放歌曲不会更新控制中心或锁定屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44642769/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com