是否有一种公共方式可以强制 MPNowPlayingInfoCenter 显示播客控件? [英] Is there a public way to force MPNowPlayingInfoCenter to show podcast controls?

查看:28
本文介绍了是否有一种公共方式可以强制 MPNowPlayingInfoCenter 显示播客控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望控制中心(通过 MPNowPlayingInfoCenter)显示 Apple 在播客中显示的前进 15 秒/后退 15 秒控件,如下所示:

I would like Control Center (via MPNowPlayingInfoCenter) to show the forward 15 seconds / back 15 seconds controls that Apple shows with podcasts, like so:

完全缺乏文档告诉我没有明显的方法可以做到这一点,但是有没有人找到任何不明显的方法来强制执行此操作而不诉诸私有方法?

The utter lack of documentation tells me that there's no obvious way to do this, but has anyone out there found any non-obvious way to force this without resorting to a private method?

我已经将前进/后退按钮的处理设置为适当地前进,我只想使用更合适的用户界面.任何帮助将不胜感激.

I've already got my handling for the forward/back button set up to advance appropriately, I'd just like to use the more appropriate UI. Any help would be greatly appreciated.

推荐答案

好的,所以我手头有一点时间,所以我跟着面包屑走....这是我发现的.

OK so I had a bit of time on my hands and so I followed the breadcrumb.… This is what I found.

包括 MediaPlayer 框架并获得 RemoteCommandCenter:

Include the MediaPlayer framework and get hold of the RemoteCommandCenter:

MPRemoteCommandCenter *rcc = [MPRemoteCommandCenter sharedCommandCenter];

然后,如果您想按照 Overcast 设置跳过控件,您可以执行以下操作:

then if you wanted to set the skip controls as per Overcast you can do the following:

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:)];

并在事件处理程序中执行您需要执行的操作以按间隔跳过:

and in the event handlers do what you need to do to skip by the interval:

-(void)skipBackwardEvent: (MPSkipIntervalCommandEvent *)skipEvent
{
    NSLog(@"Skip backward by %f", skipEvent.interval);
}

-(void)skipForwardEvent: (MPSkipIntervalCommandEvent *)skipEvent
{
    NSLog(@"Skip forward by %f", skipEvent.interval);
}

注意:preferredIntervals 属性是一个 NSArray,但我还没有弄清楚命令中心如何利用额外的间隔,除非你自己做一些事情.

Note: The preferredIntervals property is an NSArray but I haven’t figured out how extra intervals can be utilised by the command center unless you do something with this yourself.

到目前为止我发现的注意事项.执行此操作时,您将控制所有控件,因此除非您对它们执行相同操作,否则默认播放和暂停按钮不会显示:

Things to note that I’ve found so far. When you do this you are taking control of all the controls so the default play and pause buttons won't show unless you do the same for them:

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 但我无法从命令中心触发它 - 不过它确实从耳机触发.)

(there is also a togglePlayPauseCommand defined but I could’t get this to fire from the Command Centre - it does fire from headphones though.)

其他发现:按钮位于左/中/右固定位置,因此您不能拥有(例如)previousTrack 和 skipBackward,因为它们都占据左侧位置.

Other discoveries: The buttons are in fixed positions left / middle / right so you cant have (for example) a previousTrack and a skipBackward as they both occupy the left position.

seekForward/seekBackward 命令需要触发 prevTrack 和 nextTrack 命令.当您同时设置两者时,单击触发下一个/上一个,按住将触发开始搜索和抬起手指时的结束搜索.

There are seekForward / seekBackward commands that need a prevTrack and nextTrack command to be triggered. When you set up both then a single tap triggers next / previous and a press and hold triggers a begin seek and an end seek when you lift your finger.

    // 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");
    }
}

还有一个我没见过的反馈机制(占左位置)

There is also a feedback mechanism that I haven’t seen before (occupies left position)

    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");
}

这会显示三个水平条并显示一个警报表 - 您可以通过设置 active 属性来单独突出显示这些条.

This displays three horizontal bars and brings up an alert sheet - you can highlight these individually by setting the active property.

还定义了一个评级命令 - 但我无法在命令中心显示该命令

There is also a rating command defined - but I couldn't get this to show in the Command Center

//    MPRatingCommand *ratingCommand = [rcc ratingCommand];
//    [ratingCommand setEnabled:YES];
//    [ratingCommand setMinimumRating:0.0];
//    [ratingCommand setMaximumRating:5.0];
//    [ratingCommand addTarget:self action:@selector(ratingEvent:)];

和播放速率更改命令 - 但再次无法在命令中心显示此内容

and a playback rate change command - but again couldn’t get this to show in Command Center

//    MPChangePlaybackRateCommand *playBackRateCommand = [rcc changePlaybackRateCommand];
//    [playBackRateCommand setEnabled:YES];
//    [playBackRateCommand setSupportedPlaybackRates:@[@(1),@(1.5),@(2)]];
//    [playBackRateCommand addTarget:self action:@selector(remoteControlReceivedWithEvent:)];

如果您愿意,还有基于块的目标操作机制

There is also a block-based target action mechanism if you prefer

// @property (strong, nonatomic) id likeHandler;
    self.likeHandler = [likeCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
        NSLog(@"They like it");
        return MPRemoteCommandHandlerStatusSuccess;  // or fail or no such content
    }];

最后一点要注意:如果您已注册通过 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents] 接收远程事件;然后其中一些命令还会触发 - (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent 处理程序中的事件.这些是 UIEvents 虽然具有类型 UIEventTypeRemoteControl 和用于区分事件的子类型.您不能在此方法中将这些与 MPRemoteCommandEvents 混合和匹配.有迹象表明 MPRemoteCommandEvents 将在某个时候取代 UIEvents.

One final point to be aware of: If you have registered to receive remote events via [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; then some of these commands also trigger events in the - (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent handler. These are UIEvents though with type UIEventTypeRemoteControl and a subtype to distinguish the event. You can't mix and match these with MPRemoteCommandEvents in this method. There are hints that MPRemoteCommandEvents will replace the UIEvents at some point.

所有这些都基于反复试验,因此请随时纠正.

All of this based on trial and error so feel free to correct.

加雷斯

这篇关于是否有一种公共方式可以强制 MPNowPlayingInfoCenter 显示播客控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆