2015-02-08 192 views
0

我意识到这个问题之前已经被问过了,但我有和其他答案一样的代码(所以我认为)。图片和图像显示正常并更新,而新歌成为当前歌曲。我似乎无法获得播放/暂停和跳过按钮的工作。这是他使用的代码。iOS,MPNowPlayingInfoCenter暂停/播放按钮不起作用?

-(void)viewWillAppear:(bool)animated 
{ 
     [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; 
     [self becomeFirstResponder]; 
} 

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

} 
-(BOOL)canBecomeFirstResponder 
{ 
    return YES; 
} 

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

     switch (event.subtype) { 

      case UIEventSubtypeRemoteControlTogglePlayPause:[self playPauseButtonPressed:nil]; 
       NSLog(@"play pause button remote pressed"); 

       break; 

      case UIEventSubtypeRemoteControlBeginSeekingForward:[self skipButtonPressed:nil]; 
       NSLog(@"Skip remote pressed"); 
       break; 

      default: break; 
     } 
    } 
} 
+0

您发布的代码有效。只要您调用'beginReceivingRemoteControlEvents'并且您是第一响应者,您将收到远程控制事件。 – 2016-07-14 10:57:32

回答

1

使用此代码:

switch (receivedEvent.subtype) { 
     case UIEventSubtypeRemoteControlPlay: 
      [player play]; 
      break; 

     case UIEventSubtypeRemoteControlPause: 
      [player pause]; 
      break; 

     case UIEventSubtypeRemoteControlPreviousTrack: 
      [player previous]; 
      break; 

     case UIEventSubtypeRemoteControlNextTrack: 
      [player next]; 
      break; 

     default: 
      break; 
    } 

UIEventSubtypeRemoteControlBeginSeekingForward是球员寻求当前歌曲,当你长按一个/上一个按钮的时间。

确保您设置音频会话类别:

NSError *setCategoryErr = nil; 
    NSError *activationErr = nil; 
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr]; 
    [[AVAudioSession sharedInstance] setActive:YES error:&activationErr]; 

还需要设置播放速率为MPNowPlayingInfoCenter当正常显示暂停/恢复您的播放器,所以播放/暂停按钮。

 MPNowPlayingInfoCenter *playingInfoCenter = [MPNowPlayingInfoCenter defaultCenter]; 

     NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init]; 

     MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"image"]]; 

     [songInfo setObject:@"your song" forKey:MPMediaItemPropertyTitle]; 
     [songInfo setObject:@"your artist" forKey:MPMediaItemPropertyArtist]; 
     [songInfo setObject:@"your album" forKey:MPMediaItemPropertyAlbumTitle]; 
     [songInfo setObject:[NSNumber numberWithDouble:songProgress] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; 
     [songInfo setObject:[NSNumber numberWithDouble:songDuration] forKey:MPMediaItemPropertyPlaybackDuration]; 
     [songInfo setObject:[NSNumber numberWithDouble:(isPaused ? 0.0f : 1.0f)] forKey:MPNowPlayingInfoPropertyPlaybackRate]; 
     [songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork]; 

     [playingInfoCenter setNowPlayingInfo:songInfo]; 
+1

当我按下暂停按钮时,暂停不会更改为播放。我几乎试过任何解决方案让我知道的东西。 – Zulqarnain 2016-01-28 19:40:29

+0

@Zulqarnain这可能是与模拟器有关的不同问题,请参阅http://stackoverflow.com/a/38371578/829338 – 2016-07-14 10:19:23

+0

@Zulqarnain:如何解决您的问题?每次显示暂停按钮 – 2016-07-25 10:52:08