我希望通过MPNowPlayingInfoCenter控制中心显示向前15秒/向后15秒的控件,就像苹果在播客中展示的控件一样:
缺乏文档让我觉得没有明显的方法可以做到这一点,但是有没有人发现过不使用私有方法来强制实现这一点的非明显方法?
我已经设置好了向前/向后按钮的处理程序以适当地前进,我只想使用更合适的UI。 任何帮助都将不胜感激。
我希望通过MPNowPlayingInfoCenter控制中心显示向前15秒/向后15秒的控件,就像苹果在播客中展示的控件一样:
缺乏文档让我觉得没有明显的方法可以做到这一点,但是有没有人发现过不使用私有方法来强制实现这一点的非明显方法?
我已经设置好了向前/向后按钮的处理程序以适当地前进,我只想使用更合适的UI。 任何帮助都将不胜感激。
好的,我有点空闲时间,所以我跟随面包屑找到了以下内容:
包含MediaPlayer框架并获取RemoteCommandCenter:
MPRemoteCommandCenter *rcc = [MPRemoteCommandCenter sharedCommandCenter];
如果您想像Overcast一样设置跳过控件,则可以执行以下操作:
MPSkipIntervalCommand *skipBackwardIntervalCommand = [rcc skipBackwardCommand];
[skipBackwardIntervalCommand setEnabled:YES];
[skipBackwardIntervalCommand addTarget:self action:@selector(skipBackwardEvent:)];
skipBackwardIntervalCommand.preferredIntervals = @[@(42)]; // Set your own interval
MPSkipIntervalCommand *skipForwardIntervalCommand = [rcc skipForwardCommand];
skipForwardIntervalCommand.preferredIntervals = @[@(42)]; // Max 99
[skipForwardIntervalCommand setEnabled:YES];
[skipForwardIntervalCommand addTarget:self action:@selector(skipForwardEvent:)];
在事件处理程序中,做你需要做的事情以跳过间隔:
-(void)skipBackwardEvent: (MPSkipIntervalCommandEvent *)skipEvent
{
NSLog(@"Skip backward by %f", skipEvent.interval);
}
-(void)skipForwardEvent: (MPSkipIntervalCommandEvent *)skipEvent
{
NSLog(@"Skip forward by %f", skipEvent.interval);
}
MPRemoteCommand *pauseCommand = [rcc pauseCommand];
[pauseCommand setEnabled:YES];
[pauseCommand addTarget:self action:@selector(playOrPauseEvent:)];
//
MPRemoteCommand *playCommand = [rcc playCommand];
[playCommand setEnabled:YES];
[playCommand addTarget:self action:@selector(playOrPauseEvent:)];
还有一个togglePlayPauseCommand被定义了,但我无法从命令中心触发它 - 不过从耳机上可以触发。
其他发现: 按钮位置固定为左/中/右,因此您不能同时拥有(例如)previousTrack和skipBackward,因为它们都占据左侧位置。
有seekForward / seekBackward命令,需要触发prevTrack和nextTrack命令。当您设置两者时,单击触发下一个/上一个,长按触发开始搜索,松开手指后结束搜索。
// Doesn’t show unless prevTrack is enabled
MPRemoteCommand *seekBackwardCommand = [rcc seekBackwardCommand];
[seekBackwardCommand setEnabled:YES];
[seekBackwardCommand addTarget:self action:@selector(seekEvent:)];
// Doesn’t show unless nextTrack is enabled
MPRemoteCommand *seekForwardCommand = [rcc seekForwardCommand];
[seekForwardCommand setEnabled:YES];
[seekForwardCommand addTarget:self action:@selector(seekEvent:)];
-(void) seekEvent: (MPSeekCommandEvent *) seekEvent
{
if (seekEvent.type == MPSeekCommandEventTypeBeginSeeking) {
NSLog(@"Begin Seeking");
}
if (seekEvent.type == MPSeekCommandEventTypeEndSeeking) {
NSLog(@"End Seeking");
}
}
还有一个我以前没有见过的反馈机制(占据了左侧位置)
MPFeedbackCommand *likeCommand = [rcc likeCommand];
[likeCommand setEnabled:YES];
[likeCommand setLocalizedTitle:@"I love it"]; // can leave this out for default
[likeCommand addTarget:self action:@selector(likeEvent:)];
MPFeedbackCommand *dislikeCommand = [rcc dislikeCommand];
[dislikeCommand setEnabled:YES];
[dislikeCommand setActive:YES];
[dislikeCommand setLocalizedTitle:@"I hate it"]; // can leave this out for default
[dislikeCommand addTarget:self action:@selector(dislikeEvent:)];
BOOL userPreviouslyIndicatedThatTheyDislikedThisItemAndIStoredThat = YES;
if (userPreviouslyIndicatedThatTheyDislikedThisItemAndIStoredThat) {
[dislikeCommand setActive:YES];
}
MPFeedbackCommand *bookmarkCommand = [rcc bookmarkCommand];
[bookmarkCommand setEnabled:YES];
[bookmarkCommand addTarget:self action:@selector(bookmarkEvent:)];
// Feedback events also have a "negative" property but Command Center always returns not negative
-(void)dislikeEvent: (MPFeedbackCommandEvent *)feedbackEvent
{
NSLog(@"Mark the item disliked");
}
-(void)likeEvent: (MPFeedbackCommandEvent *)feedbackEvent
{
NSLog(@"Mark the item liked");
}
-(void)bookmarkEvent: (MPFeedbackCommandEvent *)feedbackEvent
{
NSLog(@"Bookmark the item or playback position");
}
这会显示三个水平条,并弹出一个警告表单 - 通过设置活动属性,您可以逐个突出显示它们。
还定义了一个评分命令 - 但我无法在命令中心中显示它。
// MPRatingCommand *ratingCommand = [rcc ratingCommand];
// [ratingCommand setEnabled:YES];
// [ratingCommand setMinimumRating:0.0];
// [ratingCommand setMaximumRating:5.0];
// [ratingCommand addTarget:self action:@selector(ratingEvent:)];
还有一个播放速度更改命令 - 但是我无法在命令中心中显示它。
// MPChangePlaybackRateCommand *playBackRateCommand = [rcc changePlaybackRateCommand];
// [playBackRateCommand setEnabled:YES];
// [playBackRateCommand setSupportedPlaybackRates:@[@(1),@(1.5),@(2)]];
// [playBackRateCommand addTarget:self action:@selector(remoteControlReceivedWithEvent:)];
// @property (strong, nonatomic) id likeHandler;
self.likeHandler = [likeCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
NSLog(@"They like it");
return MPRemoteCommandHandlerStatusSuccess; // or fail or no such content
}];
MPRemoteCommandCenter
是在 iOS 7.1 中添加的,这可能解释了为什么没有人在使用它! - pkamb面向Swift开发者
import MediaPlayer
let rcc = MPRemoteCommandCenter.shared()
let skipBackwardCommand = rcc.skipBackwardCommand
skipBackwardCommand.isEnabled = true
skipBackwardCommand.addTarget(handler: skipBackward)
skipBackwardCommand.preferredIntervals = [42]
let skipForwardCommand = rcc.skipForwardCommand
skipForwardCommand.isEnabled = true
skipForwardCommand.addTarget(handler: skipForward)
skipForwardCommand.preferredIntervals = [42]
func skipBackward(_ event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus {
guard let command = event.command as? MPSkipIntervalCommand else {
return .noSuchContent
}
let interval = command.preferredIntervals[0]
print(interval) //Output: 42
return .success
}
func skipForward(_ event: MPRemoteCommandEvent) -> MPRemoteCommandHandlerStatus {
guard let command = event.command as? MPSkipIntervalCommand else {
return .noSuchContent
}
let interval = command.preferredIntervals[0]
print(interval) //Output: 42
return .success
}
其他的命令类似,可以在这里进行查看。
由于无法更改,因此苹果没有文档。同样,苹果保留最好的东西(例如Siri)。
越狱版支持更改控制中心按钮,我在这个网站上找到了它。我有一种感觉,你想在实际的iOS 7上使用这个应用而不是越狱版,所以这对你没有帮助。
这些私有API经常妨碍开发优秀的应用程序。除非苹果给我们更多使用当前私有API的自由,否则你很遗憾。
var songInfo: NSMutableDictionary = [
MPMediaItemPropertyTitle: "song title",
MPMediaItemPropertyArtist: "artist ",
MPNowPlayingInfoPropertyElapsedPlaybackTime: "0"
]
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = songInfo as [NSObject : AnyObject]