iOS,MPNowPlayingInfoCenter 暂停/播放按钮不起作用?
<p><p>我意识到这个问题之前已经被问过,但我的代码与其他答案相同(所以我认为)。当新歌曲成为当前歌曲时,图片和图像显示良好并更新。我似乎无法让播放/暂停和跳过按钮正常工作。这是他使用的代码。 </p>
<pre><code>-(void)viewWillAppear:(bool)animated
{
[ beginReceivingRemoteControlEvents];
;
}
-(void)viewWillDisappear:(BOOL)animated
{
;
[ endReceivingRemoteControlEvents];
;
}
-(BOOL)canBecomeFirstResponder
{
return YES;
}
-(void)remoteControlReceivedWithEvent:(UIEvent *)event
{
if (event.type == UIEventTypeRemoteControl) {
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:;
NSLog(@"play pause button remote pressed");
break;
case UIEventSubtypeRemoteControlBeginSeekingForward:;
NSLog(@"Skip remote pressed");
break;
default: break;
}
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>使用此代码:</p>
<pre><code> switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlPlay:
;
break;
case UIEventSubtypeRemoteControlPause:
;
break;
case UIEventSubtypeRemoteControlPreviousTrack:
;
break;
case UIEventSubtypeRemoteControlNextTrack:
;
break;
default:
break;
}
</code></pre>
<p>UIEventSubtypeRemoteControlBeginSeekingForward 是当你长按下一个/上一个按钮时播放器寻找当前歌曲的时间。</p>
<p>确保您设置了 Audio Session 类别:</p>
<pre><code> NSError *setCategoryErr = nil;
NSError *activationErr= nil;
[ setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[ setActive:YES error:&activationErr];
</code></pre>
<p>您还需要在暂停/恢复播放器时设置 MPNowPlayingInfoCenter 的播放速率,以便正确显示播放/暂停按钮。</p>
<pre><code> MPNowPlayingInfoCenter *playingInfoCenter = ;
NSMutableDictionary *songInfo = [ init];
MPMediaItemArtwork *albumArt = [ initWithImage:];
;
;
;
forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
forKey:MPMediaItemPropertyPlaybackDuration];
forKey:MPNowPlayingInfoPropertyPlaybackRate];
;
;
</code></pre></p>
<p style="font-size: 20px;">关于iOS,MPNowPlayingInfoCenter 暂停/播放按钮不起作用?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/28389176/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/28389176/
</a>
</p>
页:
[1]