iOS AVPlayer播放模式的实现(随机播放 列表循环 单曲循环)
首先我在這里講一下我的整個播放器的思路:
首先是一個歌曲的列表,我把數(shù)據(jù)請求放在了一個單例里面,方便以后獲取每首歌曲對應(yīng)的model, 我給AVPlayer的播放也放在了一個單例里面,有播放開始,停止,等方法嗎,我通過單例的代理方法將當(dāng)前的播放時間傳給播放時的控制器,這樣控制器就可以根據(jù)傳過去的當(dāng)前時間給界面賦值了,比如播放界面的當(dāng)前時間,以及當(dāng)前的歌詞等.
實(shí)現(xiàn)播放模式的思路:
1.通過點(diǎn)擊按鈕 彈出來一個下彈窗 可以選擇播放模式 聲明一個全局變量 不同的點(diǎn)擊全局變量的值改變 全局變量默認(rèn)的播放模式是列表循環(huán)
2.播放音樂時給播放添加計時器每隔0/1秒就要響應(yīng)一次 通過代理方法傳給控制器當(dāng)前播放時間
3.在控制器的代理方法中 根據(jù)傳過來的時間與當(dāng)前歌曲的總時間對比,如果相等說明這首歌結(jié)束了,就調(diào)用歌曲結(jié)束的方法.
4.在音樂播放完畢的時候調(diào)用方法 根據(jù)不同的全局變量 實(shí)現(xiàn)不同的操作
第一步 實(shí)現(xiàn)button的點(diǎn)擊方法 通過點(diǎn)擊不同的下彈窗的值改變?nèi)肿兞?記錄選擇的模式
/ 模式typeButton的點(diǎn)擊方法的實(shí)現(xiàn) - (void)actionTypeButton:(UIButton *)typeButton {UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"請選擇模式" message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];// 添加順序播放按鈕UIAlertAction *serialAction = [UIAlertAction actionWithTitle:@"順序播放" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {// 給定義的全局變量賦值self.typeCount = 0;}];// 添加隨機(jī)播放按鈕UIAlertAction *ArcAction = [UIAlertAction actionWithTitle:@"隨機(jī)播放" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction *action) {self.typeCount = 1;}];// 添加重復(fù)播放按鈕UIAlertAction *repeatAction = [UIAlertAction actionWithTitle:@"重復(fù)播放" 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秒就要響應(yīng)一次 通過代理方法傳給控制器當(dāng)前播放時間
// 開始播放 - (void)musicPlay {self.isPlaying = YES; // 當(dāng)前的播放狀態(tài)[self.player play]; // AVPlayer開始播放self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(playingAction) userInfo:nil repeats:YES]; // 開始計時器 調(diào)用播放的響應(yīng)方法 }#pragma mark -播放過程中執(zhí)行 - (void)playingAction {// 取到當(dāng)前播放時間的秒數(shù)CGFloat time = self.player.currentTime.value / self.player.currentTime.timescale;// 代理方法 將獲取到的時間 傳遞到控制器if (self.delegate && [self.delegate performSelector:@selector(playingWithProgress:)]) {[_delegate playingWithProgress:time];}}第三步:在控制器的代理方法中 根據(jù)傳過來的時間與當(dāng)前歌曲的總時間對比,如果相等說明這首歌結(jié)束了,就調(diào)用歌曲結(jié)束的方法.
#pragma mark -- 實(shí)現(xiàn)代理方法 - (void)playingWithProgress:(CGFloat)progress {// progress 當(dāng)前歌曲播放到的時間// self.model.duration 當(dāng)前歌曲的總時間NSInteger second = self.model.duration / 1000;if (progress == second) {[self musicEnd];}}第四步:得到歌曲的進(jìn)度的值 當(dāng)播放完畢的時候 做不同的操作
#pragma mark --音樂結(jié)束后 不同模式下的反應(yīng)-- - (void)musicEnd { // progress 當(dāng)前歌曲播放到的時間// self.model.duration 當(dāng)前歌曲的總時間NSInteger second = self.model.duration / 1000;if (progress == second) {switch (self.typeCount) {case 0:{// 當(dāng)選擇列表循環(huán)時候的操作// actionDownButton: 下面有方法的實(shí)現(xiàn)[self performSelector:@selector(actionDownButton:) withObject:nil];break;}case 1:{// 當(dāng)選擇隨機(jī)播放是的操作NSInteger num = [[RootTableViewManager shareManager] getDataArrayCount];self.index = arc4random() % (num + 1);// 更改了index 就相當(dāng)于改變了model 更改了數(shù)據(jù) 所以要刷新界面[self Valuation]; // 該方法是更改界面 給AVPlayer更換playerItem(就是穿進(jìn)去新的MP3的url) 播放音樂break;}case 2:{// 當(dāng)選擇循環(huán)播放時的操作 只要更改界面就可以了[self Valuation];break;}default:break;}}}// actionDownButton:方法的實(shí)現(xiàn)
// 下一首的實(shí)現(xiàn)方法 - (void)actionDownButton:(UIButton *)downButton {self.index ++;NSInteger num = [[RootTableViewManager shareManager] getDataArrayCount];// 當(dāng)時最后一首的時候 跳到最前面if (self.index > num) {self.index = 0;}[self Valuation];}// 界面的賦值是根據(jù)model的 線面是model的實(shí)現(xiàn)
/ 重寫model的get方法 - (Model *)model {// 根據(jù)當(dāng)前的index 選中對應(yīng)index歌曲的model 成為當(dāng)前播放的數(shù)據(jù)源odel *model = [[RootTableViewManager shareManager] getModelAtIndex:self.index];return model; }總結(jié)
以上是生活随笔為你收集整理的iOS AVPlayer播放模式的实现(随机播放 列表循环 单曲循环)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言复杂程序100例,经典C语言程序1
- 下一篇: linux设计论文题目,计算机linux