在iOS 7.0设备上调用remoteControlReceivedWithEvent但不在iOS 8.0上调用 [英] remoteControlReceivedWithEvent called on iOS 7.0 device but not iOS 8.0

查看:492
本文介绍了在iOS 7.0设备上调用remoteControlReceivedWithEvent但不在iOS 8.0上调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在后台播放音频的应用程序。我正在尝试修复一个错误,其中音频控制(播放/暂停),在主屏幕(等),不在iOS 8.0+上工作,但在iOS 7.0上工作FINE。我一直在努力弄清楚问题是什么,并且一直空洞。任何想法将不胜感激。这就是我所拥有的。

I have an application that plays audio in the background. I am trying to fix a bug where the audio controls (play/pause), on the home screen (etc.), DO NOT work on iOS 8.0+ but work FINE on iOS 7.0. I have been digging through trying to figure out what the issue is and have been coming up empty. Any ideas would be greatly appreciated. Here is what I have in place.

在项目设置中:
我确保 UIBackgroundModes 设置为 audio

In the Project Settings: I have ensured that UIBackgroundModes is set to audio.

在AppDelegate.h中:
我有 AVAudioSession *会话的成员; 以及 AVPlayer * audioPlayer;

In the AppDelegate.h: I have a member for the AVAudioSession* session; as well as the AVPlayer *audioPlayer;

在AppDelegate.m中:

In the AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

self.session = [AVAudioSession sharedInstance];

NSError* error = NULL;
[self.session setCategory:AVAudioSessionCategoryPlayback error:&error];
[self.session setActive:YES error:&error];

if (error) {
    NSLog(@"AVAudioSession error: %@", [error localizedDescription]);
}

在AudioPlayerViewController.m

In the AudioPlayerViewController.m

- (void)viewDidLoad {

//Get the Audio
NSURL *url = [NSURL URLWithString:self.audioUrl];
AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];

//Setup the player
self.playerItem = [AVPlayerItem playerItemWithAsset:asset];
appDelegate.audioPlayer = [AVPlayer playerWithPlayerItem:self.playerItem];

//Setup the "Now Playing"
NSMutableDictionary *mediaInfo = [[NSMutableDictionary alloc]init];
[mediaInfo setObject:self.title forKey:MPMediaItemPropertyTitle];
[mediaInfo setObject:self.artist forKey:MPMediaItemPropertyArtist];
[mediaInfo setObject:self.album forKey:MPMediaItemPropertyAlbumTitle];
[mediaInfo setObject:[NSNumber numberWithDouble:duration ] forKey:MPMediaItemPropertyPlaybackDuration];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:mediaInfo];

}

// Process remote control events
- (void) remoteControlReceivedWithEvent:(UIEvent *)event {

NSLog(@"AudioPlayerViewController ... remoteControlReceivedWithEvent top ....subtype: %d", event.subtype);

if (event.type == UIEventTypeRemoteControl) {

    switch (event.subtype) {
        case UIEventSubtypeRemoteControlTogglePlayPause:
            [self togglePlayPause];
            break;
        case UIEventSubtypeRemoteControlPause:
            [self doPause];
            break;
        case UIEventSubtypeRemoteControlStop:
            [self doPause];
            break;
        case UIEventSubtypeRemoteControlPlay:
            [self doPlay];
            break;
        case UIEventSubtypeRemoteControlPreviousTrack:
            [self nextOrPrevTrack];
            break;
        case UIEventSubtypeRemoteControlNextTrack:
            [self nextOrPrevTrack];
            break;
        default:
            break;
    }
}

}

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

}

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

}

- (BOOL) canBecomeFirstResponder {
return YES;

}

推荐答案

终于找到了我遇到的问题。最终,似乎主屏幕上远程控制的事件从未进入我的应用程序,直到我的视图控制器。我最终继承了 UIWindow ,以便我可以看到哪些事件正在通过链。由于 UIWindow 是一个 UIResponder 我还添加了 - (void)remoteControlReceivedWithEvent :( UIEvent * )事件到子类。然后在makeKeyAndVisible中我添加了:

Finally figured out the issue I was having. Ultimately it seemed that the events from the Remote Control on the home screen were never making it into my app and down to my view controllers. I ended up subclassing the UIWindow so that I could see what events were making their way through the chain. As UIWindow is a UIResponder I also added the - (void)remoteControlReceivedWithEvent:(UIEvent *)event to the subclass. Then in the makeKeyAndVisible I added the:

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

我启动了调试器和 - (void)makeKeyAndVisible 从未被调用过!然后我在我的app委托中搜索窗口成员变量和 [window makeKeyAndVisible]; 行无处可寻!我把它添加回来(因为它应该在那里)并且presto事件正在路由到魔法等正确的位置。为什么这适用于某些版本的iOS而不是其他版本而且没有任何其他明显的问题超出了我的范围。

I started up the debugger and the -(void)makeKeyAndVisible was never called! I then searched my app delegate for the window member variable and the [window makeKeyAndVisible]; line was nowhere to be found! I added it back in (as it should have been there) and presto events are routing to the correct locations like magic. Why this was working on some versions of iOS and not others and without any other noticeable issues is beyond me.

希望这可以帮助将来的某个人。

Hope this helps someone out in the future.

这篇关于在iOS 7.0设备上调用remoteControlReceivedWithEvent但不在iOS 8.0上调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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