iOS 8,AVPlayer背景音乐播放,remoteControlReceivedWithEvent未调用

时间:2015-07-05 06:51:31

标签: swift ios8 avfoundation background-process avaudiosession

我想在我的应用中播放声音(使用swift), 在didFinishLaunchingWithOptions函数中我写了这个

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)

我有一个UIView,其中包含AVPlayer

我用过:

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

remoteControlReceivedWithEvent功能。当应用程序进入后台时,声音会继续播放,但是当我单击暂停按钮(我们用来播放和暂停应用程序外部的声音)时,remoteControlReceivedWithEvent永远不会被调用!

怎么了?

1 个答案:

答案 0 :(得分:1)

在我的项目中: 我在UITabbarViewController

中称之为
-(BOOL)canBecomeFirstResponder{
    return YES;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
}

确保它可以成为第一响应者。 remoteControlReceivedWithEvent in AVAudio is not being called