2015-02-11 38 views
1

我试图用一个班级来管理全球AVAudioPlayer实例,但我无法让锁屏控制出现。该类的子类UIResponder,但无论我尝试锁定屏幕控件都不会出现。iOS音频控件不能与单身人士一起工作

客户端类使用- (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]; 
} 
+0

如果我不得不猜测,在'setupAudioControls'之后还有其他'becomeFirstResponder's。如果这个“UIResponder”不是应用程序发送到后台时的第一响应者,我觉得这样做不起作用。 – 2015-02-11 19:22:39

+0

出于好奇,你认为即使你不播放音频,这个代码也能工作吗?我的理解是,截取遥控器事件只有在你开始播放音频时才有效,但我不清楚应该如何与锁屏遥控器进行交互。 – Palpatim 2015-02-11 20:37:30

+0

@Palpatim你可能是对的。我假设音乐还没有开始,锁屏播放按钮可以启动音乐。这可能是错误的。 – 2015-02-13 03:01:37

回答

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]; 
}