LyricsAnalysis 功能描述:鎖屏歌曲信息、控制臺遠程控制音樂播放:暫停/播放、上一首/下一首、快進/快退、鎖屏狀態下列表菜單彈框和拖拽控制臺的進度條調節進度(結合了QQ音樂和網易云音樂在鎖屏狀態下的效果)、歌詞解析并隨音樂滾動顯示。
總效果預覽圖.gif
第一部分:鎖屏效果包括:鎖屏歌曲信息和遠程控制音樂播放
① 鎖屏歌曲信息顯示
iOS11以下鎖屏信息預覽
//展示鎖屏歌曲信息:圖片、歌詞、進度、歌曲名、演唱者、專輯、(歌詞是繪制在圖片上的)
- (void)showLockScreenTotaltime:(float)totalTime andCurrentTime:(float)currentTime andLyricsPoster:(BOOL)isShow{
NSMutableDictionary * songDict = [[NSMutableDictionary alloc] init];
//設置歌曲題目
[songDict setObject:@"多幸運" forKey:MPMediaItemPropertyTitle];
//設置歌手名
[songDict setObject:@"韓安旭" forKey:MPMediaItemPropertyArtist];
//設置專輯名
[songDict setObject:@"專輯名" forKey:MPMediaItemPropertyAlbumTitle];
//設置歌曲時長
[songDict setObject:[NSNumber numberWithDouble:totalTime] forKey:MPMediaItemPropertyPlaybackDuration];
//設置已經播放時長
[songDict setObject:[NSNumber numberWithDouble:currentTime] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
UIImage * lrcImage = [UIImage imageNamed:@"backgroundImage5.jpg"];
if (isShow) {
//制作帶歌詞的海報
if (!_lrcImageView) {
_lrcImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 480,800)];
}
if (!_lockScreenTableView) {
_lockScreenTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 800 - 44 * 7 + 20, 480, 44 * 3) style:UITableViewStyleGrouped];
_lockScreenTableView.dataSource = self;
_lockScreenTableView.delegate = self;
_lockScreenTableView.separatorStyle = NO;
_lockScreenTableView.backgroundColor = [UIColor clearColor];
[_lockScreenTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cellID"];
}
//主要為了把歌詞繪制到圖片上,已達到更新歌詞的目的
[_lrcImageView addSubview:self.lockScreenTableView];
_lrcImageView.image = lrcImage;
_lrcImageView.backgroundColor = [UIColor blackColor];
//獲取添加了歌詞數據的海報圖片
UIGraphicsBeginImageContextWithOptions(_lrcImageView.frame.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[_lrcImageView.layer renderInContext:context];
lrcImage = UIGraphicsGetImageFromCurrentImageContext();
_lastImage = lrcImage;
UIGraphicsEndImageContext();
}else{
if (_lastImage) {
lrcImage = _lastImage;
}
}
//設置顯示的海報圖片
[songDict setObject:[[MPMediaItemArtwork alloc] initWithImage:lrcImage]
forKey:MPMediaItemPropertyArtwork];
//加入正在播放媒體的信息中心
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songDict];
}
② 遠程控制音樂播放
左側列表菜單彈出框.PNG
在此之前需先滿足后臺播放音樂的條件:
//后臺播放音頻設置,需要在Capabilities->Background Modes中勾選Audio,Airplay,and Picture in Picture ,如下圖1、2
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
1.png
2.png
- 在iOS7.1之前, App如果需要在鎖屏界面開啟和監控遠程控制事件,可以通過重寫- (void)remoteControlReceivedWithEvent:(UIEvent *)event這個方法來捕獲遠程控制事件,并根據event.subtype來判別指令意圖并作出反應。具體用法如下:
//在具體的控制器或其它類中捕獲處理遠程控制事件,當遠程控制事件發生時觸發該方法, 該方法屬于UIResponder類,iOS 7.1 之前經常用
- (void)remoteControlReceivedWithEvent:(UIEvent *)event{
NSLog(@"%ld",event.type);
[[NSNotificationCenter defaultCenter] postNotificationName:@"songRemoteControlNotification" object:self userInfo:@{@"eventSubtype":@(event.subtype)}];
}
/* iOS 7.1之前*/
//讓App開始接收遠程控制事件, 該方法屬于UIApplication類
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
//結束遠程控制,需要的時候關閉
// [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
//處理控制臺的暫停/播放、上/下一首事件
[[NSNotificationCenter defaultCenter] addObserverForName:@"songRemoteControlNotification" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notification) {
NSInteger eventSubtype = [notification.userInfo[@"eventSubtype"] integerValue];
switch (eventSubtype) {
case UIEventSubtypeRemoteControlNextTrack:
NSLog(@"下一首");
break;
case UIEventSubtypeRemoteControlPreviousTrack:
NSLog(@"上一首");
break;
case UIEventSubtypeRemoteControlPause:
[self.player pause];
break;
case UIEventSubtypeRemoteControlPlay:
[self.player play];
break;
//耳機上的播放暫停
case UIEventSubtypeRemoteControlTogglePlayPause:
NSLog(@"播放或暫停");
break;
//后退
case UIEventSubtypeRemoteControlBeginSeekingBackward:
break;
case UIEventSubtypeRemoteControlEndSeekingBackward:
NSLog(@"后退");
break;
//快進
case UIEventSubtypeRemoteControlBeginSeekingForward:
break;
case UIEventSubtypeRemoteControlEndSeekingForward:
NSLog(@"前進");
break;
default:
break;
}
}];
- 在iOS7.1之后,出現了MPRemoteCommandCenter、MPRemoteCommand 及其相關的一些類 ,鎖屏界面開啟和監控遠程控制事件就更方便了,而且還擴展了一些新功能:網易云音樂的列表菜單彈框功能和QQ音樂的拖拽控制臺的進度條調節進度功能等等.....
官方文檔:https://developer.apple.com/documentation/mediaplayer/mpremotecommandcenter
//鎖屏界面開啟和監控遠程控制事件
- (void)createRemoteCommandCenter{
/**/
//遠程控制命令中心 iOS 7.1 之后 詳情看官方文檔:https://developer.apple.com/documentation/mediaplayer/mpremotecommandcenter
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
// MPFeedbackCommand對象反映了當前App所播放的反饋狀態. MPRemoteCommandCenter對象提供feedback對象用于對媒體文件進行喜歡, 不喜歡, 標記的操作. 效果類似于網易云音樂鎖屏時的效果
//添加喜歡按鈕
MPFeedbackCommand *likeCommand = commandCenter.likeCommand;
likeCommand.enabled = YES;
likeCommand.localizedTitle = @"喜歡";
[likeCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
NSLog(@"喜歡");
return MPRemoteCommandHandlerStatusSuccess;
}];
//添加不喜歡按鈕,假裝是“上一首”
MPFeedbackCommand *dislikeCommand = commandCenter.dislikeCommand;
dislikeCommand.enabled = YES;
dislikeCommand.localizedTitle = @"上一首";
[dislikeCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
NSLog(@"上一首");
return MPRemoteCommandHandlerStatusSuccess;
}];
//標記
MPFeedbackCommand *bookmarkCommand = commandCenter.bookmarkCommand;
bookmarkCommand.enabled = YES;
bookmarkCommand.localizedTitle = @"標記";
[bookmarkCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
NSLog(@"標記");
return MPRemoteCommandHandlerStatusSuccess;
}];
// commandCenter.togglePlayPauseCommand 耳機線控的暫停/播放
[commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
[self.player pause];
return MPRemoteCommandHandlerStatusSuccess;
}];
[commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
[self.player play];
return MPRemoteCommandHandlerStatusSuccess;
}];
// [commandCenter.previousTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
// NSLog(@"上一首");
// return MPRemoteCommandHandlerStatusSuccess;
// }];
[commandCenter.nextTrackCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
NSLog(@"下一首");
return MPRemoteCommandHandlerStatusSuccess;
}];
//快進
// MPSkipIntervalCommand *skipBackwardIntervalCommand = commandCenter.skipForwardCommand;
// skipBackwardIntervalCommand.preferredIntervals = @[@(54)];
// skipBackwardIntervalCommand.enabled = YES;
// [skipBackwardIntervalCommand addTarget:self action:@selector(skipBackwardEvent:)];
//在控制臺拖動進度條調節進度(仿QQ音樂的效果)
[commandCenter.changePlaybackPositionCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
CMTime totlaTime = self.player.currentItem.duration;
MPChangePlaybackPositionCommandEvent * playbackPositionEvent = (MPChangePlaybackPositionCommandEvent *)event;
[self.player seekToTime:CMTimeMake(totlaTime.value*playbackPositionEvent.positionTime/CMTimeGetSeconds(totlaTime), totlaTime.timescale) completionHandler:^(BOOL finished) {
}];
return MPRemoteCommandHandlerStatusSuccess;
}];
}
-(void)skipBackwardEvent: (MPSkipIntervalCommandEvent *)skipEvent
{
NSLog(@"快進了 %f秒", skipEvent.interval);
}
第二部分:歌詞解析
歌詞樣式.png
- 根據上圖的歌詞樣式,思路就是:先根據換行符“\n“分割字符串,獲得包含每一行歌詞字符串的數組,然后解析每一行歌詞字符,獲得時間點和對應的歌詞,再用創建的歌詞對象wslLrcEach來存儲時間點和歌詞,最后得到一個存儲wslLrcEach對象的數組。
//每句歌詞對象
@interface wslLrcEach : NSObject
@property(nonatomic, assign) NSUInteger time ;
@property(nonatomic, copy) NSString * lrc ;
@end
接下來就是要讓歌詞隨歌曲的進度來滾動顯示,主要代碼如下:
self.tableView 顯示歌詞的
currentTime 當前播放時間點
self.currentRow 當前時間點歌詞的位置
//歌詞滾動顯示
for ( int i = (int)(self.lrcArray.count - 1); i >= 0 ;i--) {
wslLrcEach * lrc = self.lrcArray[i];
if (lrc.time < currentTime) {
self.currentRow = i;
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow: self.currentRow inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
[self.tableView reloadData];
break;
}
}
- 更新于2017/9/13 iOS11系統正式發布后 , iOS11上不能像iOS11以下那樣鎖屏歌詞和海報,iOS11把海報顯示位置放到了左上方,而且大小變成了頭像大小,可能是蘋果為了鎖屏界面的簡潔,只保留了如下圖的界面。
iOS11網易云音樂鎖屏界面.PNG
- 更新于2018/3/7 :上面提到 iOS11系統上 ,不能像以往那樣顯示鎖屏歌詞了,那鎖屏歌詞該怎么顯示呢,網易云音樂給出了如下圖的設計:她是把當前唱到的歌詞放到了鎖屏的副標題處,隨著播放的進度而改變。
[songDict setObject:@"當前歌詞" forKey:MPMediaItemPropertyAlbumTitle];
網易云音樂鎖屏歌詞.PNG
- 更新于2018/8/2
? ? ? ? 最近有小猿反應了一個Bug:鎖屏下暫停播放,過幾秒再繼續播放,進度條會跳一下,暫停越久跳越猛?
? ? ? ?查閱資料后發現:MPNowPlayingInfoCenter中的進度刷新并不是由app不停的更新nowPlayingInfo來做的,而是根據app傳入的ElapsedPlaybackTime和PlaybackRate進行自動刷新。
? ? ? ? 我們知道 player 有個 rate 屬性,表示播放速率,為 0 的時候表示暫停,為 1.0 的時候表示播放,而MPNowPlayingInfoCenter的nowPlayingInfo也有一個鍵值MPNowPlayingInfoPropertyPlaybackRate表示速率rate,但是它 與 self.player.rate 是不同步的,也就是說[self.player pause]暫停播放后的速率rate是0,但MPNowPlayingInfoPropertyPlaybackRate還是1,就會造成 在鎖屏界面點擊了暫停按鈕,這個時候進度條表面看起來停止了走動,但是其實還是在計時,所以再點擊播放的時候,鎖屏界面進度條的光標會發生位置閃動, 所以我們需要在播放狀態改變時同步播放速率給MPNowPlayingInfoPropertyPlaybackRate。
[songDict setObject:[NSNumber numberWithInteger:rate] forKey:MPNowPlayingInfoPropertyPlaybackRate];
好了,就說這么多了,demo中注釋的還算是清楚的,感興趣的可以去look look????!
GitHub:LyricsAnalysis,覺得有幫助的話,別忘了給個star??哈????!
如果需要跟我交流的話:
※ Github: https://github.com/wsl2ls
※ 掘金:https://juejin.im/user/5c00d97b6fb9a049fb436288
※ 簡書:http://www.lxweimin.com/u/e15d1f644bea
※ 微信公眾號:iOS2679114653
※ QQ:1685527540
親,贊一下,給個star.gif