日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

iOS AVPlayer播放模式的实现(随机播放 列表循环 单曲循环)

發布時間:2023/12/31 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS AVPlayer播放模式的实现(随机播放 列表循环 单曲循环) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先我在這里講一下我的整個播放器的思路:
首先是一個歌曲的列表,我把數據請求放在了一個單例里面,方便以后獲取每首歌曲對應的model, 我給AVPlayer的播放也放在了一個單例里面,有播放開始,停止,等方法嗎,我通過單例的代理方法將當前的播放時間傳給播放時的控制器,這樣控制器就可以根據傳過去的當前時間給界面賦值了,比如播放界面的當前時間,以及當前的歌詞等.

實現播放模式的思路:
1.通過點擊按鈕 彈出來一個下彈窗 可以選擇播放模式 聲明一個全局變量 不同的點擊全局變量的值改變 全局變量默認的播放模式是列表循環
2.播放音樂時給播放添加計時器每隔0/1秒就要響應一次 通過代理方法傳給控制器當前播放時間
3.在控制器的代理方法中 根據傳過來的時間與當前歌曲的總時間對比,如果相等說明這首歌結束了,就調用歌曲結束的方法.
4.在音樂播放完畢的時候調用方法 根據不同的全局變量 實現不同的操作

第一步 實現button的點擊方法 通過點擊不同的下彈窗的值改變全局變量,記錄選擇的模式

/ 模式typeButton的點擊方法的實現 - (void)actionTypeButton:(UIButton *)typeButton {UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"請選擇模式" message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];// 添加順序播放按鈕UIAlertAction *serialAction = [UIAlertAction actionWithTitle:@"順序播放" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {// 給定義的全局變量賦值self.typeCount = 0;}];// 添加隨機播放按鈕UIAlertAction *ArcAction = [UIAlertAction actionWithTitle:@"隨機播放" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {self.typeCount = 1;}];// 添加重復播放按鈕UIAlertAction *repeatAction = [UIAlertAction actionWithTitle:@"重復播放" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {self.typeCount = 2;}];[alertController addAction:serialAction];[alertController addAction:ArcAction];[alertController addAction:repeatAction];[self presentViewController:alertController animated:YES completion:nil]; }

第二步:給播放添加計時器每隔0/1秒就要響應一次 通過代理方法傳給控制器當前播放時間

// 開始播放 - (void)musicPlay {self.isPlaying = YES; // 當前的播放狀態[self.player play]; // AVPlayer開始播放self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(playingAction) userInfo:nil repeats:YES]; // 開始計時器 調用播放的響應方法 }#pragma mark -播放過程中執行 - (void)playingAction {// 取到當前播放時間的秒數CGFloat time = self.player.currentTime.value / self.player.currentTime.timescale;// 代理方法 將獲取到的時間 傳遞到控制器if (self.delegate && [self.delegate performSelector:@selector(playingWithProgress:)]) {[_delegate playingWithProgress:time];}}

第三步:在控制器的代理方法中 根據傳過來的時間與當前歌曲的總時間對比,如果相等說明這首歌結束了,就調用歌曲結束的方法.

#pragma mark -- 實現代理方法 - (void)playingWithProgress:(CGFloat)progress {// progress 當前歌曲播放到的時間// self.model.duration 當前歌曲的總時間NSInteger second = self.model.duration / 1000;if (progress == second) {[self musicEnd];}}

第四步:得到歌曲的進度的值 當播放完畢的時候 做不同的操作

#pragma mark --音樂結束后 不同模式下的反應-- - (void)musicEnd { // progress 當前歌曲播放到的時間// self.model.duration 當前歌曲的總時間NSInteger second = self.model.duration / 1000;if (progress == second) {switch (self.typeCount) {case 0:{// 當選擇列表循環時候的操作// actionDownButton: 下面有方法的實現[self performSelector:@selector(actionDownButton:) withObject:nil];break;}case 1:{// 當選擇隨機播放是的操作NSInteger num = [[RootTableViewManager shareManager] getDataArrayCount];self.index = arc4random() % (num + 1);// 更改了index 就相當于改變了model 更改了數據 所以要刷新界面[self Valuation]; // 該方法是更改界面 給AVPlayer更換playerItem(就是穿進去新的MP3的url) 播放音樂break;}case 2:{// 當選擇循環播放時的操作 只要更改界面就可以了[self Valuation];break;}default:break;}}}

// actionDownButton:方法的實現

// 下一首的實現方法 - (void)actionDownButton:(UIButton *)downButton {self.index ++;NSInteger num = [[RootTableViewManager shareManager] getDataArrayCount];// 當時最后一首的時候 跳到最前面if (self.index > num) {self.index = 0;}[self Valuation];}

// 界面的賦值是根據model的 線面是model的實現

/ 重寫model的get方法 - (Model *)model {// 根據當前的index 選中對應index歌曲的model 成為當前播放的數據源odel *model = [[RootTableViewManager shareManager] getModelAtIndex:self.index];return model; }

總結

以上是生活随笔為你收集整理的iOS AVPlayer播放模式的实现(随机播放 列表循环 单曲循环)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。