ios - remoteControlReceivedWithEvent 在 iOS 7.0 设备上调用,但在 iOS 8.0 上不调用

标签 ios ios8 avplayer ios8.1

我有一个在后台播放音频的应用程序。我正在尝试修复主屏幕(等)上的音频控件(播放/暂停)在 iOS 8.0+ 上不起作用但在 iOS 7.0 上运行良好的错误。我一直在努力找出问题所在,但一无所获。任何想法将不胜感激。这是我所拥有的。

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

在 AppDelegate.h 中: 我有一个成员用于 AVAudioSession* session; 以及 AVPlayer *audioPlayer;

在 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 中

- (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;

最佳答案

终于解决了我遇到的问题。最终,主屏幕上远程控制的事件似乎从未进入我的应用程序,也没有进入我的 View Controller 。我最终将 UIWindow 子类化,这样我就可以看到哪些事件正在通过链。由于 UIWindow 是一个 UIResponder,我还向子类添加了 - (void)remoteControlReceivedWithEvent:(UIEvent *)event。然后在 makeKeyAndVisible 中我添加了:

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

我启动了调试器,但从未调用过 -(void)makeKeyAndVisible!然后,我在我的应用程序委托(delegate)中搜索了 window 成员变量,但找不到 [window makeKeyAndVisible]; 行!我把它加回去了(因为它本来应该在那里的),并且 presto 事件像魔术一样路由到正确的位置。为什么这适用于某些版本的 iOS 而不是其他版本并且没有任何其他明显的问题超出了我的范围。

希望这对以后的人有所帮助。

关于ios - remoteControlReceivedWithEvent 在 iOS 7.0 设备上调用,但在 iOS 8.0 上不调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27006282/

相关文章:

ios - AVPlayerLayer 与 AVPlayerViewController

ios - Swift - 如何在表格 View 单元格中显示视频的缩略图捕获?

ios - 在哪里指定我的应用程序支持的 iOS 版本

ios - 如何更改我的返回方法上的可观察类型

iphone - 我可以在 XCode 4.3.2 中禁用 "Upgrade debugger from GDB to LLDB"警告吗?

ios - 如何在 Swift 中调用 animateAlongsideTransition?

ios - 有没有办法在 IOS swift 中播放 .amr 文件?

ios - 图片库图片网址“assets-library://asset/asset.JPG?id = 1000000007&ext = JPG”为零

ios - 检测自定义键盘是否已安装

objective-c - 在 swift3.2/4 中正确解除分配 MTAudioProcessingTapRef