簡介
使用FreeStreamer實現了一個流媒體播放器,同時還實現了歌詞同步,鎖屏控制,歌曲切換,后臺自動播放等功能。
演示效果
總覽.gif
附上項目地址chenfengxiaoxixi
實現技術點及流程
1.單例
首先播放器所在controller我是使用單例初始化的,不然pop到上一級控制器后,當前對象釋放掉,就無法播放了
+ (instancetype)sharePlayerController
{
static CFPlayerController *_instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_instance = [[self alloc] init];
});
return _instance;
}
這個方法是線程安全的。
2.歌詞同步
歌詞同步.gif
這里重點是歌詞格式的問題,才開始我用的純歌詞文本,很難精確定位到播放到哪段歌詞了,后來網上查了下資料,才知道用lrc格式的歌詞定位,格式:[時間]+歌詞,如下
[00:21.70]客官里面請幾位
[00:24.72]請上座好茶來奉陪
[00:27.71]一雙筷從南吃到北
[00:30.73]中華五千年留下的韻味
分別用數組把時間和歌詞文本存下來,然后用tableview顯示出來。在播放進度里面,用正在播放的時間和歌詞文本的時間段對比,然后標記當前正在播放哪段歌詞。
3.后臺持續播放
先在xcode配置里面(TARGETS->Capabilities)打開Background Modes,勾選上Audio那一欄。現在只是滿足了后臺播放條件,要想連續不斷在后臺播放,還要申請后臺任務id。
//添加后臺播放任務
UIBackgroundTaskIdentifier bgTask = 0;
if([UIApplication sharedApplication].applicationState== UIApplicationStateBackground) {
NSLog(@"后臺播放");
UIApplication*app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier newTask = [app beginBackgroundTaskWithExpirationHandler:nil];
if(bgTask!= UIBackgroundTaskInvalid) {
[app endBackgroundTask: bgTask];
}
bgTask = newTask;
[self next];
}
else {
NSLog(@"前臺播放");
[self.cdView scrollRightWIthNext];
}
播放完成一首歌后,這段代碼用來判斷當前處于前臺還是后臺,如果是后臺,那就申請后臺任務繼續播放下一首。
4.鎖屏后對音樂播放的操作及信息顯示
鎖屏演示.gif
這里我使用的MPRemoteCommandCenter實現的功能,不過該類的部分方法只支持ios9.1版本以上。
配置要實現的操作
- (void)configRemoteControlEvents
{
// [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
//初始化遠程控制中心
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];
if (@available(iOS 9.1, *)) {
//播放,暫停
[commandCenter.togglePlayPauseCommand addTarget:self action:@selector(remoteControlActionWithPlayPause:)];
//下一首
[commandCenter.nextTrackCommand addTarget:self action:@selector(remoteControlActionWithNext:)];
//上一首
[commandCenter.previousTrackCommand addTarget:self action:@selector(remoteControlActionWithPre:)];
//進度條拖動
[commandCenter.changePlaybackPositionCommand addTarget:self action:@selector(remoteControlActionSeekToPosition:)];
} else {
// Fallback on earlier versions
}
}
更新鎖屏后音樂的顯示信息
//鎖屏顯示信息
- (void)configNowPlayingInfoCenter
{
if (NSClassFromString(@"MPNowPlayingInfoCenter")) {
NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
[dict setObject:CFUSER.currentSongModel.songName forKey:MPMediaItemPropertyTitle];
[dict setObject:@(self.playTime) forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
//音樂的總時間
[dict setObject:@(self.totalTime) forKey:MPMediaItemPropertyPlaybackDuration];
//當前歌詞添加在副標題處
if (_lyricsManager.currentRow <= _lyricsManager.textArray.count - 1) {
[dict setObject:_lyricsManager.textArray[_lyricsManager.currentRow] forKey:MPMediaItemPropertyAlbumTitle];
}
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];
}
}
5.關于FreeStreamer的使用
初始化,開始播放
//初始化播放器
- (void)buildStreamer
{
weakSELF;
// 網絡文件
NSURL *url = [NSURL URLWithString:CFUSER.currentSongModel.url];
if (!_audioStream) {
// 創建FSAudioStream對象
_audioStream = [[FSAudioStream alloc] initWithUrl:url];
_audioStream.onFailure = ^(FSAudioStreamError error,NSString *description){
NSLog(@"播放過程中發生錯誤,錯誤信息:%@",description);
[weakSelf showAlertMsg:description];
};
_audioStream.onCompletion=^(){
[weakSelf autoPlayNext];
};
[_audioStream preload];
[_audioStream play];
}
}
停止播放
[self.audioStream stop];
暫停播放和繼續播放為同一個方法
[self.audioStream pause];
拖動進度條播放
- (void)dragSliderEnd:(UISlider *)slider{
//滑動到底時,播放下一曲
if (slider.value == 1) {
[self.cdView scrollRightWIthNext];
}
else
{
if (slider.value > 0)
{
//初始化一個FSStreamPosition結構體
FSStreamPosition pos;
//只對position賦值,value由slider控制
pos.position = slider.value;
[self.audioStream seekToPosition:pos];// 到指定位置播放
}
}
}
結語
以上實現了一個簡單的流媒體音樂播放器,代碼里面有比較詳細的注釋,想看詳細實現流程和源碼的可以去我的項目查看。