AVAudio中的remoteControlReceivedWithEvent未被调用

时间:2011-07-27 11:06:23

标签: iphone avaudioplayer

我正在开发一个在后台播放音频的应用。我按照app docs的指示编写了iPod控件的代码。我已经实现了这个

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {    
    if (receivedEvent.type == UIEventTypeRemoteControl) {

        switch (receivedEvent.subtype) {

            case UIEventSubtypeRemoteControlTogglePlayPause:
                [self playButtonPressed:playButton];
                [self testing];
                break;

            case UIEventSubtypeRemoteControlPreviousTrack:
                [self rewButtonReleased:(UIButton *)rewButton];
                break;

            case UIEventSubtypeRemoteControlNextTrack:
                [self ffwButtonReleased:(UIButton *)ffwButton];
                break;

            default:
                break;
        }
    }
}
- (BOOL)canBecomeFirstResponder {
    NSLog(@"canBecomeFirstResponder");
    return YES;
}

- (void) viewDidAppear:(BOOL)animated 
{
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [self becomeFirstResponder];
    }
}

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

请告诉我我还需要什么,或者我在哪里犯错误。此致!

5 个答案:

答案 0 :(得分:9)

确保它可以成为第一响应者

- (BOOL)canBecomeFirstResponder {
     return YES;
}

答案 1 :(得分:5)

我花了好几个小时试图解决同样的问题,直到找到类似的问题:How can you play music from the iPod app while still receiving remote control events in your app?

我的问题是我正在启用混音覆盖,试图让背景音频工作:

UInt32 doSetProperty = 1;
AudioSessionSetProperty (kAudioSessionProperty_OverrideCategoryMixWithOthers,
                     sizeof (doSetProperty), &doSetProperty);

我删除了这些行(事实证明它们不是背景音频所必需的),并且remoteControlReceivedWithEvent开始工作。这有点道理。如果您指定您的应用程序应该能够将其音频与当前在其他应用程序中播放的任何内容混合(例如,将应用程序的声音效果与iPod应用程序的音乐混合),那么其他应用程序应该保留远程按钮按下的优先级

答案 2 :(得分:1)

您是否已将UIBackgroundModes音频密钥添加到info.plist中?请参阅docs

答案 3 :(得分:0)

除了将UIBackgroundModes音频键添加到您应用的info.plist之外,我还添加了:

-(void)viewDidLoad
{
    UIDevice* device = [UIDevice currentDevice];

    BOOL backgroundSupported = NO;

    if ([device respondsToSelector:@selector(isMultitaskingSupported)])
    {
        backgroundSupported = device.multitaskingSupported;
    }

......你的代码......

答案 4 :(得分:0)

在Swift 3中,请确保:

override var canBecomeFirstResponder: Bool {

    get {

        return true
    }
}