我还没有找到在iOS 7的锁定屏幕上如何倒带歌曲的方法。音量滑块可用,所有按钮正常工作,但上部滑块不可用!
我使用的是AVPlayer类。
非常感谢任何帮助!
我还没有找到在iOS 7的锁定屏幕上如何倒带歌曲的方法。音量滑块可用,所有按钮正常工作,但上部滑块不可用!
我使用的是AVPlayer类。
非常感谢任何帮助!
你需要在“现在播放信息”中进行设置。
每当项目(曲目)更改时,就执行类似以下操作:
NSMutableDictionary *nowPlayingInfo = [[NSMutableDictionary alloc] init];
[nowPlayingInfo setObject:track.artistName forKey:MPMediaItemPropertyArtist];
[nowPlayingInfo setObject:track.trackTitle forKey:MPMediaItemPropertyTitle];
...
[nowPlayingInfo setObject:[NSNumber numberWithDouble:self.player.rate] forKey:MPNowPlayingInfoPropertyPlaybackRate];
[nowPlayingInfo setObject:[NSNumber numberWithDouble:self.currentPlaybackTime] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nowPlayingInfo;
每当播放速率改变(播放/暂停)时,执行以下操作:
NSMutableDictionary *nowPlayingInfo = [[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo mutableCopy];
[nowPlayingInfo setObject:[NSNumber numberWithDouble:self.player.rate] forKey:MPNowPlayingInfoPropertyPlaybackRate];
[nowPlayingInfo setObject:[NSNumber numberWithDouble:self.currentPlaybackTime] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nowPlayingInfo;
您可以像这样获取当前播放时间:
- (NSTimeInterval)currentPlaybackTime {
CMTime time = self.player.currentTime;
if (CMTIME_IS_VALID(time)) {
return time.value / time.timescale;
}
return 0;
}
您需要在Swift中注册不同的事件,如下所示:
MPRemoteCommandCenter.shared().playCommand.addTarget(self, action: #selector(self.handleRemoteCommandActions))
MPRemoteCommandCenter.shared().nextTrackCommand.addTarget(self, action: #selector(self.handleRemoteCommandActions))
MPRemoteCommandCenter.shared().previousTrackCommand.addTarget(self, action: #selector(self.handleRemoteCommandActions))
MPRemoteCommandCenter.shared().stopCommand.addTarget(self, action: #selector(self.handleRemoteCommandActions))
MPRemoteCommandCenter.shared().pauseCommand.addTarget(self, action: #selector(self.handleRemoteCommandActions))
MPRemoteCommandCenter.shared().changePlaybackPositionCommand.isEnabled = true
MPRemoteCommandCenter.shared().changePlaybackPositionCommand.addTarget(self, action: #selector(self.handleChangePlaybackPositionRemoteCommandActions))
let rcc = MPRemoteCommandCenter.shared()
let skipBackwardIntervalCommand: MPSkipIntervalCommand? = rcc.skipBackwardCommand
skipBackwardIntervalCommand?.isEnabled = false
let skipForwardIntervalCommand: MPSkipIntervalCommand? = rcc.skipForwardCommand
skipForwardIntervalCommand?.isEnabled = false
let seekForwardCommand: MPRemoteCommand? = rcc.seekForwardCommand
seekForwardCommand?.isEnabled = true
rcc.changePlaybackPositionCommand.isEnabled = true
rcc.changePlaybackRateCommand.isEnabled = true
rcc.ratingCommand.isEnabled = true
rcc.playCommand.isEnabled = true
rcc.togglePlayPauseCommand.isEnabled = true
在这里,handleChangePlaybackPositionRemoteCommandActions方法将是您管理歌曲搜索时必须使用的方法,当滑块(上部滑块)更改其值时。
它会像下面这样:
@objc func handleChangePlaybackPositionRemoteCommandActions(event:MPChangePlaybackPositionCommandEvent) -> MPRemoteCommandHandlerStatus
{
print("handleChangePlaybackPositionRemoteCommandActions : \(event.positionTime)")
self.appDelegate.audioPlayer?.seek(to: CMTime(seconds: event.positionTime, preferredTimescale: (self.appDelegate.audioPlayer?.currentItem?.currentTime().timescale)!))
MPNowPlayingInfoCenter.default().nowPlayingInfo![MPNowPlayingInfoPropertyElapsedPlaybackTime] = event.positionTime
return MPRemoteCommandHandlerStatus.success
}
以下是如何使用时间轴滑块完成此操作的方法:
首先,必须正确设置nowPlayingInfo
。您可以在任何地方找到如何执行此操作(不要忘记关键字MPNowPlayingInfoPropertyPlaybackRate
);
其次是定义“滑动”操作:
MPChangePlaybackPositionCommand *changePlaybackPositionCommand = [[MPRemoteCommandCenter sharedCommandCenter] changePlaybackPositionCommand];
[changePlaybackPositionCommand addTarget:self action:@selector(changePlaybackPositionEvent:)];
[[MPRemoteCommandCenter sharedCommandCenter].changePlaybackPositionCommand setEnabled:YES];
在这个操作中,您可以进行一些其他的事情,但主要的目标是
[yourPlayer seekTo:event.positionTime completionHandler:^(BOOL finished) {
[self updatePlaybackRateMetadata];
}];
updatePlaybackRateMetadata
方法必须更新MPNowPlayingInfoPropertyElapsedPlaybackTime
和MPNowPlayingInfoPropertyPlaybackRate
。
虽然时间过去了很久,但我希望这可以帮助到某些人!
[nowPlayingInfo setObject:[track valueForProperty: MPMediaItemPropertyPlaybackDuration]
forKey:MPMediaItemPropertyPlaybackDuration];
- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent
if (receivedEvent.subtype == UIEventSubtypeRemoteControlBeginSeekingBackward){};
if (receivedEvent.subtype == UIEventSubtypeRemoteControlBeginSeekingForward){};
if (receivedEvent.subtype == UIEventSubtypeRemoteControlEndSeekingBackward){};
if (receivedEvent.subtype == UIEventSubtypeRemoteControlEndSeekingForward){};
BeginSeekingForward和BeginSeekingBackward是由前进和后退按钮的长按触发的,当手指松开按钮时,EndSeekingForward和EndSeekingBackward事件子类型自然而然地被触发。
当您获得这些事件子类型时,请传递一个新的NSDictionary。
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo
使用所有已有的信息(否则未传递的属性将被清除),再加上一个新的MPNowPlayingInfoPropertyElapsedPlaybackTime
和MPNowPlayingInfoPropertyPlaybackRate
。对于速率,您可以决定音频滚动的速度。1是正常速度,负值向后播放,因此-2是倒放的2倍正常速度。您必须将MPNowPlayingInfoCenter
的播放速率设置为与AVPlayer
的播放速率相同,否则它们将不匹配。这就是为什么您还要传递当前时间的原因。您可以使用以下方法获取当前时间:
[player currentTime]
[nowPlayingInfo setObject:[player currentTime]
forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
[nowPlayingInfo setObject:[[NSNumber alloc] initWithFloat:10]
forKey:MPNowPlayingInfoPropertyPlaybackRate];
[player setRate:10]
MPNowplayingInfo
可以与任何播放器一起使用,用AVPlayer
实现即可。 - chris