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\];

相关推荐
开心就好202513 小时前
UniApp开发应用多平台上架全流程:H5小程序iOS和Android
后端·ios
开心就好202516 小时前
免 Xcode 的 iOS 开发新选择?聊聊一款更轻量的 iOS 开发 IDE kxapp 快蝎
后端·ios
恋猫de小郭19 小时前
Apple 的 ANE 被挖掘,AI 硬件公开,宣传的 38 TOPS 居然是"数字游戏"?
前端·人工智能·ios
忆江南2 天前
iOS 深度解析
flutter·ios
没有故事的Zhang同学2 天前
05-主题|事件响应者链@iOS-应用场景与进阶实践
ios
FeliksLv2 天前
尝试给Lookin 支持 MCP
ios
没有故事的Zhang同学2 天前
01-研究系统框架@Web@iOS | JavaScriptCore 框架:从使用到原理解析
ios
CocoaKier3 天前
苹果谷歌商店:如何监控并维护用户评分评论
ios·google·apple
iOS日常4 天前
iOS设备崩溃日志获取与查看
ios·xcode
wangruofeng4 天前
AI 助力 Flutter 3.27 升级到 3.38 完整指南:两周踩坑与实战复盘
flutter·ios·ai编程