在iOS 7.0设备上调用remoteControlReceivedWithEvent,但在iOS 8.0设备上未调用remoteControlReceivedWithEvent

在iOS 7.0设备上调用remoteControlReceivedWithEvent,但在iOS 8.0设备上未调用remoteControlReceivedWithEvent,ios,ios8,avplayer,ios8.1,Ios,Ios8,Avplayer,Ios8.1,我有一个在后台播放音频的应用程序。我正在尝试修复一个错误,即主屏幕上的音频控件(播放/暂停)在iOS 8.0+上无法工作,但在iOS 7.0上可以正常工作。我一直在钻研,试图找出问题所在,结果却一无所获。任何想法都将不胜感激。这就是我所拥有的 在项目设置中: 我已确保ui背景模式设置为音频 在AppDelegate.h中: 我有一名会员参加AVAudioSession*会议以及AVPlayer*音频播放器 在AppDelegate.m中: - (BOOL)application:(UIAppli

我有一个在后台播放音频的应用程序。我正在尝试修复一个错误,即主屏幕上的音频控件(播放/暂停)在iOS 8.0+上无法工作,但在iOS 7.0上可以正常工作。我一直在钻研,试图找出问题所在,结果却一无所获。任何想法都将不胜感激。这就是我所拥有的

在项目设置中: 我已确保
ui背景模式
设置为
音频

在AppDelegate.h中: 我有一名会员参加
AVAudioSession*会议
以及
AVPlayer*音频播放器

在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]);
}
在audioplayervewcontroller.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];
}

}

}

}


}

终于解决了我的问题。最终,主屏幕上遥控器的事件似乎从未进入我的应用程序,也没有进入我的视图控制器。最后,我对
ui窗口进行了子类化,这样我就可以看到哪些事件在链中传递。由于
UIWindow
UIResponder
我还将
-(void)remoteControlReceivedWithEvent:(UIEvent*)事件添加到子类中。然后在makeKeyAndVisible中,我添加了:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
我启动了调试器,
-(void)makeKeyAndVisible
从未被调用过!然后,我在我的应用程序委托中搜索
窗口
成员变量和
[window makeKeyAndVisible]行没有找到!我把它加了回去(因为它应该在那里),presto事件会像magic一样路由到正确的位置。我无法理解为什么这在某些版本的iOS上有效,而在其他版本上无效,并且没有任何其他值得注意的问题


希望这对将来的人有所帮助

在ViewController中添加

override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
        self.becomeFirstResponder()
}

override func remoteControlReceivedWithEvent(event: UIEvent) {
        // your stuff
    }
在AppDelegate中添加

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

var error: NSError?
        AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: &error)
        AVAudioSession.sharedInstance().setActive(true, error: &error)
}
SWIFT 3

UIApplication.shared.beginReceivingRemoteControlEvents()
self.becomeFirstResponder()

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    try AVAudioSession.sharedInstance().setActive(true)
} catch {
    print("hmmm...")
}

你能发布一些关于你是如何工作的相关代码吗?例如,您是如何以及在何处对
UIWindow
进行子类化的?问题在于缺少[window makeKeyAndVisible]调用。与UIWindow类的子类化无关。当您有多个ViewController(其中一个是它们的播放器)并切换到另一个ViewController时会发生什么。您如何继续处理PlayerController中现在已“销毁”的遥控器?好的方面:我认为您应该在AppDelegate或Singleton对象中处理此类行为,这是正确的答案。添加了逻辑AppDelegate以调用视图控制器中的方法,因为播放器是静态的。
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)

        UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
        self.becomeFirstResponder()
}

override func remoteControlReceivedWithEvent(event: UIEvent) {
        // your stuff
    }
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

var error: NSError?
        AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: &error)
        AVAudioSession.sharedInstance().setActive(true, error: &error)
}
UIApplication.shared.beginReceivingRemoteControlEvents()
self.becomeFirstResponder()

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    try AVAudioSession.sharedInstance().setActive(true)
} catch {
    print("hmmm...")
}