iOS鎖屏音訊播放控制及音訊資訊設定
阿新 • • 發佈:2020-01-07
iOS 後臺音訊播放控制,鎖屏音訊播放控制及音訊資訊設定,效果圖如下:
1.在AppDelegate.m 中實現下面方法,獲取音訊播放、暫停、上一首、下一首點選事件:
- (BOOL)canBecomeFirstResponder { return YES; } //鎖屏介面控制監聽 - (void)remoteControlReceivedWithEvent:(UIEvent *)event { if (event.type == UIEventTypeRemoteControl) { switch (event.subtype) { case UIEventSubtypeRemoteControlPlay: { //播放 NSLog(@"Play"); break; } case UIEventSubtypeRemoteControlPause: { //暫停 NSLog(@"Pause"); break; } case UIEventSubtypeRemoteControlNextTrack: { //下一首 NSLog(@"Next"); break; } case UIEventSubtypeRemoteControlPreviousTrack: { //上一首 NSLog(@"Previous"); break; } default: break; } } }
2.設定鎖屏資訊:
//設定鎖屏資訊 - (void)setLockingInfo { Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter"); if (playingInfoCenter) { //音訊模型 HWMusicModel *model = [HWMusicTool playingMusic]; //資料資訊 NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init]; //圖片 MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageWithUrlString:model.icon]]; [songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork]; //當前播放時間 [songInfo setObject:[NSNumber numberWithDouble:[[[HWMusicTool shareMusicTool] Player] currentPlaybackTime]] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime]; //速率 [songInfo setObject:[NSNumber numberWithFloat:1.0f] forKey:MPNowPlayingInfoPropertyPlaybackRate]; //剩餘時長 [songInfo setObject:[NSNumber numberWithDouble:[[[HWMusicTool shareMusicTool] Player] duration]] forKey:MPMediaItemPropertyPlaybackDuration]; //設定標題 [songInfo setObject:model.title forKey:MPMediaItemPropertyTitle]; //設定副標題 [songInfo setObject:@"周杰倫 - 周杰倫的床邊故事" forKey:MPMediaItemPropertyArtist]; //設定音訊資料資訊 [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo]; } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。