1

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

客户端类用于- (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];
}
4

1 回答 1

1

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

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

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

  [self updateMediaInfo];
}
于 2015-02-11T20:27:31.753 回答