RemoteCommandCenter

作者: Ylang | 来源:发表于2015-11-19 10:52 被阅读1601次

播放音乐控制器


4201750.png

1.引用框架MediaPlayer,使使用RemoteCommandCenter,版本需求iOS7.1
MPRemoteCommandCenter *rcc = [MPRemoteCommandCenter sharedCommandCenter];

2.RemoteCommandCenter几种情况
设置前进和倒退

MPSkipIntervalCommand *skipBackwardIntervalCommand = [rcc skipBackwardCommand];
[skipBackwardIntervalCommand setEnabled:YES];
[skipBackwardIntervalCommand addTarget:self action:@selector(skipBackwardEvent:)];
  skipBackwardIntervalCommand.preferredIntervals = @[@(42)];  // 设置快进时间
  
  MPSkipIntervalCommand *skipForwardIntervalCommand = [rcc skipForwardCommand];
  skipForwardIntervalCommand.preferredIntervals = @[@(42)];  // 倒退时间 最大 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:)];
    
    
    MPRemoteCommand *nextCommand = [rcc nextTrackCommand];
    [nextCommand setEnabled:YES];
    [nextCommand addTarget:self action:@selector(nextCommandEvent:)];

- (void)playOrPauseEvent:(MPRemoteCommand *)command
{
    if (avAudioPlayer.isPlaying)
    {
        [avAudioPlayer pause];
    }else
    {
        [avAudioPlayer play];
    }
    NSLog(@"%@",command);
}
- (void)nextCommandEvent:(MPRemoteCommand *)command
{
   NSLog(@"%@",@"下一曲");
}

使用Feedback 来控制

-(void)feedbackCommand:(MPRemoteCommandCenter *)rcc
{
    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:)];
    
    MPFeedbackCommand *bookmarkCommand = [rcc bookmarkCommand];
    [bookmarkCommand setEnabled:YES];
    [bookmarkCommand addTarget:self action:@selector(bookmarkEvent:)];
}

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

评分

-(void)reat:(MPRemoteCommandCenter *)rcc
{
        MPRatingCommand *ratingCommand = [rcc ratingCommand];
        [ratingCommand setEnabled:YES];
        [ratingCommand setMinimumRating:0.0];
        [ratingCommand setMaximumRating:5.0];
        [ratingCommand addTarget:self action:@selector(ratingEvent:)];
}
-(void)ratingEvent:(MPRatingCommand *)commd
{
    
}
-(void)playBackRate:(MPRemoteCommandCenter *)rcc
{
    MPChangePlaybackRateCommand *playBackRateCommand = [rcc changePlaybackRateCommand];
    [playBackRateCommand setEnabled:YES];
    [playBackRateCommand setSupportedPlaybackRates:@[@(1),@(1.5),@(2)]];
    [playBackRateCommand addTarget:self action:@selector(remoteControlReceivedWithEvent:)];
}
- (void)remoteControlReceivedWithEvent:(MPChangePlaybackRateCommand*)rate
{
    
}

也可以使用简单的方式

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

ps:如果已经通过注册 [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];也会触发该事件-remoteControlReceivedWithEvent:, UIEvent 和UIEventTypeRemoteControl 的事件要进行区分,根据目前的发展情况 MPRemoteCommandEvents将取代UIEvents的一些功能.

相关文章

网友评论

本文标题:RemoteCommandCenter

本文链接:https://www.haomeiwen.com/subject/qcjlhttx.html