iOS 音频控件不适用于单例

iOS audio controls not working with singleton

我正在尝试使用 class 来管理全局 AVAudioPlayer 实例,但无法显示锁定屏幕控件。 class subclasses UIResponder,但无论我尝试什么锁屏控件都不会出现。

客户端 class 使用 - (void)setUrl:(NSURL *)url withTitle:(NSString *)title 加载音频并使用 playAudio 播放。我在这里做错了什么?

我将后台执行权限设置为音频和汽车播放,即使在模拟器 > 重置内容和设置后,这仍然失败。

- (void)playAudio {
    [self.audioPlayer play];
    [self updateNowPlayingInfo];
}

- (void)stopAudio {
    [self.audioPlayer stop];
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nil];
}

- (void)pauseAudio {
    [self.audioPlayer pause];
    [self updateNowPlayingInfo];
}

- (void)setupAudioSession {
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    NSError *setCategoryError = nil;
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
    NSError *activationError = nil;
    [audioSession setActive:YES error:&activationError];
}

- (void)setupAudioControls {
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
    [self updateNowPlayingInfo];
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
    if (receivedEvent.type == UIEventTypeRemoteControl) {
        switch (receivedEvent.subtype) {
            case UIEventSubtypeRemoteControlTogglePlayPause:
                NSLog(@"Play pause");
                if (self.audioPlayer.isPlaying) {
                    [self.audioPlayer pause];
                }
                else {
                    [self.audioPlayer play];
                }
                break;

            case UIEventSubtypeRemoteControlPreviousTrack:
                break;

            case UIEventSubtypeRemoteControlNextTrack:
                break;

            default:
                break;
        }

        [self updateNowPlayingInfo];
    }
}

- (void)updateNowPlayingInfo {
    NSMutableDictionary *albumInfo = [[NSMutableDictionary alloc] init];
    albumInfo[MPMediaItemPropertyTitle] = self.title;
    albumInfo[MPMediaItemPropertyArtist] = @"Fidelity";
    albumInfo[MPMediaItemPropertyAlbumTitle] = self.title;
    albumInfo[MPMediaItemPropertyPlaybackDuration] = @(self.audioPlayer.duration);
    albumInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = @(self.audioPlayer.currentTime);
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:albumInfo];
}

- (void)setUrl:(NSURL *)url withTitle:(NSString *)title {
    self.title = title;
    self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [self setupAudioSession];
    [self setupAudioControls];
}

您的 -(void)setupAudioControls 应该在 进入后台后立即被调用 。在我的应用程序中:

...
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(appDidEnterBackground:)
                                               name:UIApplicationDidEnterBackgroundNotification
                                             object:nil];
...

- (void)appDidEnterBackground:(NSNotification *)notification {
  [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
  [self becomeFirstResponder];

  [self updateMediaInfo];
}