您可能希望使用较新的 MPRemoteCommandCenter,而不是使用 beginReceivingRemoteControlEvents。例如:
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
[commandCenter.togglePlayPauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
NSLog(@"toggle button pressed");
return MPRemoteCommandHandlerStatusSuccess;
}];
或者,如果您更喜欢使用方法而不是块:
[commandCenter.togglePlayPauseCommand addTarget:self action:@selector(toggleButtonAction)];
停止:
[commandCenter.togglePlayPauseCommand removeTarget:self];
或者:
[commandCenter.togglePlayPauseCommand removeTarget:self action:@selector(toggleButtonAction)];
您需要将其添加到文件的包含区域:
@import MediaPlayer;