才不是什么黑科技呢,哼哒~

一段时间前就看见某公司的音乐App宣传自己的工程师用黑科技在iOS的锁屏界面添加了一些小功能,昨天晚上又有人提到。

其实这个真不是什么黑科技,是iOS的Public API啊,只不过Apple的文档里对这个API的描述略少,实际中也很少有App使用,于是就被说成黑科技了,说成黑科技有什么好处呢,好处自然多了,特别是对于商业公司。

于是这里就给出一个如何正确使用的例子吧,这个东西才不是什么黑科技呢,哼哒~

如果你的App不需要" 喜欢/书签"等功能的话,直接使用RemoteControl就行。

对于RemoteCommand,一个SEL只能对应一个command。

另外就是RemoteCommand的callback会先于RemoteControl,不过也就几ms的间隔。而且使用RemoteControl更方便。

//
//  ViewController.m
//  mprc
//
//  Created by BlueCocoa on 15/4/6.
//  Copyright (c) 2015年 0xBBC. All rights reserved.
//
 
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
 
@interfaceViewController ()<AVAudioPlayerDelegate>
@property (nonatomic, strong) AVAudioPlayer * player;
@property (nonatomic, strong) MPRemoteCommand * mprcNext, * mprcPlay, * mprcPause, * mprcPrevious;

@property (nonatomic, strong) MPFeedbackCommand * mpfbLike, * mpfbDislike, * mpfbBookMark;
@property (nonatomic) BOOL bookmark;
@end
 
@implementation ViewController

@synthesize player;
@synthesize mprcNext,mprcPlay,mprcPause,mprcPrevious;
@synthesize mpfbLike,mpfbDislike,mpfbBookMark;
@synthesize bookmark;
 
- (void)viewDidLoad {
    [super viewDidLoad];
    NSError *error;
    self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURLfileURLWithPath:[[NSBundle mainBundle] pathForResource:@"1" ofType:@"m4a"]] error:&error];
    if (error) {
        NSLog(@"%@",error);
        return;
    }
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    [session setActive:YES error:nil];
    [self.player setDelegate:self];
    [self.player prepareToPlay];
    [self.player play];
    [self.player setNumberOfLoops:-1];
    [self.player setVolume:1];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
 
    self.mprcNext = [MPRemoteCommandCenter sharedCommandCenter].nextTrackCommand;
    [self.mprcNext addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    [self.mprcNext setEnabled:YES];
    [self.mprcNext addTarget:self action:@selector(nextTrackCommand:)];
    self.mprcPlay = [MPRemoteCommandCenter sharedCommandCenter].playCommand;
    [self.mprcPlay addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    [self.mprcPlay setEnabled:YES];
    [self.mprcPlay addTarget:self action:@selector(playCommand:)];
    self.mprcPause = [MPRemoteCommandCenter sharedCommandCenter].pauseCommand;
    [self.mprcPause addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    [self.mprcPause setEnabled:YES];
    [self.mprcPause addTarget:self action:@selector(pauseCommand:)];
    
    self.mprcPrevious = [MPRemoteCommandCenter sharedCommandCenter].previousTrackCommand;
    [self.mprcPrevious addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    [self.mprcPrevious setEnabled:YES];
    [self.mprcPrevious addTarget:self action:@selector(previousTrackCommand:)];
    self.mpfbLike = [MPRemoteCommandCenter sharedCommandCenter].likeCommand;
    [self.mpfbLike addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    [self.mpfbLike setEnabled:YES];
    [self.mpfbLike addTarget:self action:@selector(likeCommand:)];
    [self.mpfbLike setLocalizedTitle:@"喜欢这首歌"];
    [self.mpfbLike setLocalizedShortTitle:@"喜欢"];

    self.mpfbDislike =[MPRemoteCommandCenter sharedCommandCenter].dislikeCommand;
    [self.mpfbDislike addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    [self.mpfbDislike addTarget:self action:@selector(dislikeCommand:)];
    [self.mpfbDislike setEnabled:YES];
    [self.mpfbDislike setLocalizedTitle:@"不喜欢这首歌"];
    [self.mpfbDislike setLocalizedShortTitle:@"不喜欢"];

    self.mpfbBookMark = [MPRemoteCommandCenter sharedCommandCenter].bookmarkCommand;
    [self.mpfbBookMark addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {
        return MPRemoteCommandHandlerStatusSuccess;
    }];
    [self.mpfbBookMark setEnabled:YES];
    [self.mpfbBookMark addTarget:self action:@selector(bookmarkCommand:)];
    [self.mpfbBookMarksetLocalizedTitle:@"书签"];
    [self.mpfbBookMarksetLocalizedShortTitle:@"书签"];
    self.bookmark = NO;
}

- (void)bookmarkCommand:(MPFeedbackCommandEvent *)sender
{
    self.bookmark = !self.bookmark;
    NSLog(@"Bookmark");
}

- (void)dislikeCommand:(MPFeedbackCommandEvent *)sender
{
    [self.mpfbDislike setEnabled:NO];
    [self.mpfbLike setEnabled:YES];
    NSLog(@"Dislike");
}

- (void)likeCommand:(MPFeedbackCommandEvent *)sender
{
    [self.mpfbLike setEnabled:NO];
    [self.mpfbDislikesetEnabled:YES];
    NSLog(@"Like");
}

- (void)previousTrackCommand:(MPRemoteCommandEvent *)sender
{
    NSLog(@"Previous Track");
}

- (void)playCommand:(MPRemoteCommandEvent *)sender
{
    NSLog(@"Play in MPRemoteCommandEvent");
}

- (void)pauseCommand:(MPRemoteCommandEvent *)sender
{
    NSLog(@"Pause in MPRemoteCommandEvent");
}

- (void)nextTrackCommand:(MPRemoteCommandEvent *)sender
{
    NSLog(@"Next Track");
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    switch (event.subtype) {
        case UIEventSubtypeRemoteControlPlay:
            NSLog(@"Play in RemoteControlEvent");
            break;
        case UIEventSubtypeRemoteControlPause:
            NSLog(@"Pause in RemoteControlEvent");
            break;
        default:
            break;
    }
}
@end
 
声明: 本文为0xBBC原创, 转载注明出处喵~

《才不是什么黑科技呢,哼哒~》有1个想法

发表评论

电子邮件地址不会被公开。 必填项已用*标注