iOS 语音循环播放设置

本地文件、网络文件,区别就是URL创建方式

1、使用AVPlayerItem与AVPlayer(这个简单,只需要设置回调),而不是AVAudioPlayer(这个麻烦,需要设置代理、计时器等等)

2、设置AVPlayerItem的url,然后AVPlayer播放这个AVPlayerItem,在AVPlayer播放AVPlayerItem回调读取播放完毕后,重新设置进度,具体如图

NSString *document=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];

NSString *audioPath = [document stringByAppendingPathComponent:@"voice"];

NSString *toUrl = [audioPath stringByAppendingPathComponent:self.filepatch];

NSURL *musicURL = [NSURL fileURLWithPath:toUrl];

NSError *error;

self.player = [[AVPlayer alloc] initWithPlayerItem:nil];

AVPlayerItem * songItem = [[AVPlayerItem alloc]initWithURL:musicURL];

__weak typeof(songItem) weakSong =songItem;

self.player replaceCurrentItemWithPlayerItem:songItem\]; self.player.volume = 1.0; weakify(self) \[_player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(1, 1) queue:NULL usingBlock:\^(CMTime time) { //进度 当前时间/总时间 wSelf.progress.progress =CMTimeGetSeconds(wSelf.player.currentItem.currentTime)/CMTimeGetSeconds(wSelf.player.currentItem.duration); NSLog(@"per:%f-%f",CMTimeGetSeconds(wSelf.player.currentItem.currentTime),CMTimeGetSeconds(wSelf.player.currentItem.duration)); if ( wSelf.progress.progress==1) { \[weakSong seekToTime:kCMTimeZero\]; \[wSelf.player play\]; } wSelf.label.text = \[NSString stringWithFormat:@"%.2f秒\|%.2f秒",CMTimeGetSeconds(wSelf.player.currentItem.currentTime),CMTimeGetSeconds(wSelf.player.currentItem.duration)\]; }\]; \[self.player play\];

相关推荐
Magnetic_h7 小时前
【iOS】方法与消息底层分析
笔记·学习·macos·ios·objective-c·cocoa
MaoJiu14 小时前
Flutter与原生端的通信
flutter·ios
iReaShare16 小时前
如何将 iPhone 备份到云端:完整指南
ios
小赵小赵福星高照~17 小时前
iOS UI视图面试相关
ui·ios·面试
杂雾无尘20 小时前
苹果高管揭示苹果背后秘密:苹果为何不涉足搜索引擎领域?
ios·apple
二流小码农20 小时前
鸿蒙开发:一键更新,让应用无需提交应用市场即可下载安装
android·ios·harmonyos
2501_915921431 天前
没有Mac如何完成iOS 上架:iOS App 上架App Store流程
android·ios·小程序·https·uni-app·iphone·webview
初级代码游戏2 天前
Maui劝退:用windows直接真机调试iOS,无须和Mac配对
macos·ios·配置·maui·热重载
fundroid2 天前
Swift 进军 Android,Kotlin 该如何应对?
android·ios
形影相吊3 天前
iOS防截屏实战
ios