无法接收remoteControlReceivedWithEvent - objective c - ios [英] Unable to receive remoteControlReceivedWithEvent - objective c - ios

查看:147
本文介绍了无法接收remoteControlReceivedWithEvent - objective c - ios的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功启用了我的应用,可以在屏幕锁定后在后台播放音频和视频。但是,为了获得更好的用户体验,我想在锁定的屏幕上显示正在运行的媒体的播放和暂停控制。在线跟踪几个博客后,添加了以下代码:

I successfully enabled my app to be able to play audio and video in background after the screen is locked. However, for better user experience I want to show play and pause controls of the running media on the locked screen. After following couple of blogs online, added the following code:

@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;
    }
}
}

在信息中添加了背景模式。 plist

Added background modes in info.plist

即使我能看到在我的应用程序点击按钮。

Even though I am able to see the control screen, no user event is received by my app upon clicking the buttons.

我相信我错过了在非常明显的事情上。任何指针都会有所帮助。

I believe I am missing out on something very obvious. Any pointers would be helpful.

编辑1: iOS - 未收到UIEventTypeRemoteControl事件表示您的应用必须是正在播放的应用。我该怎么做?

EDIT 1: The accepted answer in iOS - UIEventTypeRemoteControl events not received says that Your app must be the "Now Playing" app. How do I do this?

推荐答案

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

I found the answer to my question. I need to implement the code in my question in AppDelegate to receive events instead of implementing in ViewController.

这篇关于无法接收remoteControlReceivedWithEvent - objective c - ios的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆