ios遠(yuǎn)程控制事件(藍(lán)牙音箱)

最近公司在做一個(gè)關(guān)于外接藍(lán)牙音箱的功能,上網(wǎng)搜索了相關(guān)資料,發(fā)現(xiàn)大都雷同,今天做個(gè)稍許總結(jié):

1.利用遠(yuǎn)程控制事件- (void)remoteControlReceivedWithEvent:(UIEvent *)event方法

這個(gè)方法系統(tǒng)會(huì)自動(dòng)調(diào)用,可以放在AppDelegate.m文件里,也可以放在相關(guān)控制器里,前提是需要設(shè)置

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents],接收遠(yuǎn)程事件,

接收完畢后結(jié)束接收[[UIApplication sharedApplication] endReceivingRemoteControlEvents];

接收到遠(yuǎn)程事件如播放,停止,會(huì)調(diào)用remoteControlReceivedWithEvent:方法,利用改方法里的event參數(shù)判斷是什么類(lèi)型的事件:

-(void)remoteControlReceivedWithEvent:(UIEvent *)event

? ? if (event.type == UIEventTypeRemoteControl) {

? ? ? ? ?if (event.subtype == UIEventSubtypeRemoteControlPlay) {

? ? ? ? ? ? ? //....做一些事情.如播放

? ? ? ? ? ? ?} else if(//...){

? ? ? ? ? ? ? // ...

? ? ? ? ? ? ?}

? ? }

}


2.利用MPRemoteCommandCenter類(lèi)

該類(lèi)也會(huì)在系統(tǒng)接收到遠(yuǎn)程事件后被調(diào)用,前提是要添加相關(guān)事件的監(jiān)聽(tīng),并且該類(lèi)是一個(gè)單例,如:

MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

[commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {

? ? NSLog(@"暫停事件");

? ? //暫停的相關(guān)事情

? ? return MPRemoteCommandHandlerStatusSuccess;

}];

[commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {

? ? NSLog(@"播放事件");

? ? //播放相關(guān)事情

? ? return MPRemoteCommandHandlerStatusSuccess;

}];

在不用接收遠(yuǎn)程事件時(shí)要記的移除監(jiān)聽(tīng):

MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

[commandCenter.pauseCommand removeTarget:self];

[commandCenter.playCommand removeTarget:self];

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容