我意识到这个问题之前已经被问过,但我的代码与其他答案相同(所以我认为)。当新歌曲成为当前歌曲时,图片和图像显示良好并更新。我似乎无法让播放/暂停和跳过按钮正常工作。这是他使用的代码。
-(void)viewWillAppearbool)animated
{
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
-(void)viewWillDisappearBOOL)animated
{
[super viewWillDisappear:YES];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}
-(BOOL)canBecomeFirstResponder
{
return YES;
}
-(void)remoteControlReceivedWithEventUIEvent *)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;
}
}
}
Best Answer-推荐答案 strong>
使用此代码:
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 是当你长按下一个/上一个按钮时播放器寻找当前歌曲的时间。
确保您设置了 Audio Session 类别:
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 numberWithDoubleisPaused ? 0.0f : 1.0f)] forKey:MPNowPlayingInfoPropertyPlaybackRate];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
[playingInfoCenter setNowPlayingInfo:songInfo];
关于iOS,MPNowPlayingInfoCenter 暂停/播放按钮不起作用?,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/28389176/
|