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 allocinitWithURL: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;

相关推荐
hurrycry_小亦9 小时前
苹果WWDC 2026前瞻:Ferret-Pro端侧大模型即将亮相|小亦之闻|AI 编程三日速递!(5月26日~5月28日)
macos·ios·wwdc
UTF_813 小时前
一次NSMutableAttributedString误用的思考
ios·面试·github
人月神话-Lee17 小时前
【图像处理】Core Image 与 GPU 渲染管线——让滤镜飞起来
图像处理·人工智能·ios·chatgpt·ai编程·swift·gpu
夏天的峰没有风21 小时前
Typora+gitcode+picgo搭建免费图床
开发语言·ios·swift
库奇噜啦呼21 小时前
【iOS】源码学习-分类、扩展、关联对象
学习·ios·分类
帅次1 天前
Android 17 开发者实战:核心更新与应用场景落地指南
android·java·ios·android studio·iphone·android jetpack·webview
人月神话Lee1 天前
【图像处理】Core Image 与 GPU 渲染管线——让滤镜飞起来
ios·ai编程·图像识别
帅次2 天前
讯飞与腾讯云:Android 实时语音识别服务对比选择
android·ios·微信小程序·小程序·android studio·android runtime
择势2 天前
用一套View代码,同时支持RTL和LTR布局混合排版
ios
游戏开发爱好者82 天前
iOS开发工具推荐:Xcode、AppCode、SwiftLint使用心得与效率提升
ide·vscode·macos·ios·个人开发·xcode·敏捷流程