2015-02-08 189 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