iOS-VLCKit實(shí)現(xiàn)仿網(wǎng)易云音樂(lè)播放音樂(lè)(一)

iOS仿網(wǎng)易云音樂(lè)

iOS-VLCKit實(shí)現(xiàn)仿網(wǎng)易云音樂(lè)播放音樂(lè)(一)

iOS-VLCKit實(shí)現(xiàn)仿網(wǎng)易云音樂(lè)播放音樂(lè)(二)

iOS-VLCKit實(shí)現(xiàn)仿網(wǎng)易云音樂(lè)播放音樂(lè)(三)

前言

最近做項(xiàng)目遇到需要播放音頻的功能,通過(guò)查找資料,最終選擇了VLCKit來(lái)實(shí)現(xiàn)。
VLC - 一款功能強(qiáng)大的全平臺(tái)播放器,幾乎支持所有格式的音頻、視頻文件的播放
集成方式
1、 按照wiki的說(shuō)明去自己編譯:[https://wiki.videolan.org/iOSCompile]
2、cocoapods方式
通過(guò)pos search MobileVLCKit去搜索相關(guān)的庫(kù),會(huì)發(fā)現(xiàn)有好幾個(gè)庫(kù),我最終選擇了MobileVLCKit-unstable(因?yàn)檫@個(gè)庫(kù)更新的多,而且還在不時(shí)的更新)

pod 'MobileVLCKit-unstable', '~> 3.0.0a40'

說(shuō)明

本Demo是根據(jù)VLCKit播放庫(kù)寫(xiě)的仿網(wǎng)易云播放界面。

主要實(shí)現(xiàn)的功能有:

* 播放網(wǎng)絡(luò)音頻、歌曲
* 歌詞滾動(dòng)、音量控制、歌曲切換
* 設(shè)置循環(huán)類(lèi)型、上一曲、下一曲、喜歡歌曲等
* 鎖屏控制(播放、暫停、喜歡、上一曲、下一曲、播放條拖動(dòng))
* 耳機(jī)線控(播放、暫停、上一曲、下一曲、快進(jìn)、快退)
* 通知監(jiān)聽(tīng)(插拔耳機(jī)、播放打斷)

不足:

* 不能獲取緩沖進(jìn)度(播放庫(kù)的問(wèn)題)
* 暫停后繼續(xù)播放聲音不準(zhǔn)確(播放庫(kù)的問(wèn)題)
* airplay暫未支持

demo中的音樂(lè)文件來(lái)自百度音樂(lè),僅供學(xué)習(xí)使用,請(qǐng)勿在商業(yè)中使用

部分功能的主要實(shí)現(xiàn)

1、歌詞解析

+ (NSArray *)lyricParaseWithLyricString:(NSString *)lyricString {
    // 1. 以\n分割歌詞
    NSArray *linesArray = [lyricString componentsSeparatedByString:@"\n"];

    // 2. 創(chuàng)建模型數(shù)組
    NSMutableArray *modelArray = [NSMutableArray new];

    // 3. 開(kāi)始解析
    for (NSString *line in linesArray) {
        // 正則表達(dá)式 [00:01.78], \\ 轉(zhuǎn)義,  @"\\[\\d{2}:\\d{2}.\\d{2}\\]"
        NSString *pattern = @"\\[[0-9][0-9]:[0-9][0-9].[0-9][0-9]\\]";

        NSRegularExpression *regular = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
        // 進(jìn)行匹配
        NSArray *matchesArray = [regular matchesInString:line options:NSMatchingReportProgress range:NSMakeRange(0, line.length)];

        // 方法二  [00:01.78]歌詞
        NSString *content = [line componentsSeparatedByString:@"]"].lastObject;

        // 獲取時(shí)間部分[00:00.00]
        for (NSTextCheckingResult *match in matchesArray) {
            NSString *timeStr = [line substringWithRange:match.range];
            // 去掉開(kāi)頭和結(jié)尾的[],得到時(shí)間00:00.00
            timeStr = [timeStr substringWithRange:NSMakeRange(1, 8)];
            // 分、秒、毫秒
            NSString *minStr = [timeStr substringWithRange:NSMakeRange(0, 2)];
            NSString *secStr = [timeStr substringWithRange:NSMakeRange(3, 2)];
            NSString *mseStr = [timeStr substringWithRange:NSMakeRange(6, 2)];

            // 轉(zhuǎn)換成以毫秒秒為單位的時(shí)間 1秒 = 1000毫秒
            NSTimeInterval time = [minStr floatValue] * 60 * 1000 + [secStr floatValue] * 1000 + [mseStr floatValue];

            // 創(chuàng)建模型,賦值
            GKLyricModel *lyricModel = [GKLyricModel new];
            lyricModel.content      = content;
            lyricModel.msTime       = time;
            lyricModel.secTime      = time / 1000;
            lyricModel.timeString   = [GKTool timeStrWithMsTime:time];
            [modelArray addObject:lyricModel];
        }
    }

    // 數(shù)組根據(jù)時(shí)間進(jìn)行排序 時(shí)間(time)
    // ascending: 是否升序
    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"msTime" ascending:YES];

    return [modelArray sortedArrayUsingDescriptors:@[descriptor]];
}

2、歌詞滾動(dòng)

- (void)scrollLyricWithCurrentTime:(NSTimeInterval)currentTime {
    if (self.lyricList.count == 0) self.lyricIndex = 0;

    for (NSInteger i = 0; i < self.lyricList.count; i++) {
        GKLyricModel *currentLyric = self.lyricList[i];
        GKLyricModel *nextLyric = nil;
        if (i < self.lyricList.count - 1) {
            nextLyric = self.lyricList[i + 1];
        }
        if ((self.lyricIndex != i && currentTime >= currentLyric.msTime) && (!nextLyric || currentTime < nextLyric.msTime)) {
            self.lyricIndex = i;

            [self.lyricTable reloadData];
            [self.lyricTable selectRowAtIndexPath:[NSIndexPath indexPathForRow:(self.lyricIndex + 5) inSection:0] animated:YES scrollPosition:UITableViewScrollPositionMiddle];
        }
    }
}

3、鎖屏功能控制

// 喜歡、上一曲
// 喜歡按鈕
MPFeedbackCommand *likeCommand = commandCenter.likeCommand;
likeCommand.enabled        = YES;
likeCommand.active         = self.model.isLike;
likeCommand.localizedTitle = self.model.isLike ? @"取消喜歡" : @"喜歡";
[likeCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
    // 喜歡
    return MPRemoteCommandHandlerStatusSuccess;
}];
// 上一首
MPFeedbackCommand *dislikeCommand = commandCenter.dislikeCommand;
dislikeCommand.enabled = YES;
dislikeCommand.localizedTitle = @"上一首";
[dislikeCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {

    NSLog(@"上一首");

    [self playPrevMusic];

    return MPRemoteCommandHandlerStatusSuccess;
}];

// 拖動(dòng)進(jìn)度條
if ([UIDevice currentDevice].systemVersion.doubleValue >= 9.0) {
    [commandCenter.changePlaybackPositionCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {

            MPChangePlaybackPositionCommandEvent *positionEvent = (MPChangePlaybackPositionCommandEvent *)event;

            if (positionEvent.positionTime != self.positionTime) {
                self.positionTime = positionEvent.positionTime;

                self.currentTime = self.positionTime * 1000;

                kPlayer.progress = (float)self.currentTime / self.duration;
            }

        return MPRemoteCommandHandlerStatusSuccess;
    }];
}

部分界面截圖

我的音樂(lè)
播放界面
播放列表
鎖屏圖片
鎖屏控制

github地址:GKAudioPlayerDemo

最后編輯于
?著作權(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)容

  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一種新的協(xié)議。它實(shí)...
    香橙柚子閱讀 24,701評(píng)論 8 183
  • 實(shí)現(xiàn)這個(gè)功能需要注意兩點(diǎn): 1.設(shè)置文本框的高度 2.高度為行高的整數(shù)倍,顯示兩行,則高度為行高的2倍 需要用到的...
    orangesup閱讀 1,034評(píng)論 0 3
  • 世間是否有完美的生活,也許有,也許沒(méi)有,主要取決于我們對(duì)生活要求,如果我們的要求一味的超出力所能及的范圍,那...
    夜已空閱讀 342評(píng)論 0 3
  • 你遇到的每一個(gè)人都是自己 我要改變,我太不喜歡我目前的工作了,我也要像你一樣自由。 不,親愛(ài)的,如果你真的希望夢(mèng)想...
    秋的彩虹閱讀 271評(píng)論 0 0

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