gpt4 book ai didi

ios - 无法接收 remoteControlReceivedWithEvent - objective-c - ios

转载 作者:可可西里 更新时间:2023-11-01 04:43:10 27 4
gpt4 key购买 nike

我成功地使我的应用程序能够在屏幕锁定后在后台播放音频和视频。但是,为了更好的用户体验,我想在锁定屏幕上显示正在运行的媒体的播放和暂停控件。在网上关注了几篇博客后,添加了以下代码:

@interface MyControllerClass () <UIGestureRecognizerDelegate, UIApplicationDelegate>

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];

AVURLAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:avAsset];
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
NSError *activationError = nil;
BOOL success = [[AVAudioSession sharedInstance] setActive: YES error: &activationError];
}

- (void)viewWillDisappear:(BOOL)animated {
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
[super viewWillDisappear:animated];
}

- (BOOL) canBecomeFirstResponder {
return YES;
}

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
NSLog(@"received event %@",receivedEvent);
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause: {
if ([self isVideoPlaying]) {
[self.avPlayer pause];
} else {
[self.avPlayer play];
}
break;
}
case UIEventSubtypeRemoteControlPlay: {
[self.avPlayer play];
break;
}
case UIEventSubtypeRemoteControlPause: {
[self.avPlayer pause];
break;
}
default:
break;
}
}
}

在 info.plist 中添加背景模式

enter image description here

即使我能够看到控制屏幕,但在单击按钮后我的应用程序没有收到任何用户事件 enter image description here .

我相信我错过了一些非常明显的东西。任何指示都会有所帮助。

编辑 1:iOS - UIEventTypeRemoteControl events not received 中接受的答案表示您的应用必须是“正在播放”应用。我该怎么做?

最佳答案

我找到了问题的答案。我需要在 AppDelegate 中实现问题中的代码以接收事件,而不是在 ViewController 中实现。

关于ios - 无法接收 remoteControlReceivedWithEvent - objective-c - ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43973602/

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