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;

相关推荐
CocoaKier12 小时前
Facebook登录的这条红色警告,我终于搞明白了,真是反人类设计!
ios·facebook
humors22115 小时前
【转帖】安全企业发布iOS漏洞攻击风险检测工具
安全·ios·手机·苹果
沐风___19 小时前
iOS 崩溃收集实战:从「只看到次数、看不到原因」到 Sentry 完整集成
ios·sentry
listening77720 小时前
HarmonyOS 6.1 跨端实战:用ArkUI-X把电商App同时跑在Android/iOS上
android·ios·harmonyos
Tyler_120 小时前
iOS PlayWright mcp server
前端·ios·ai编程
2501_916008892 天前
iOS 证书管理最佳实践 从创建到续期的完整指南
android·ios·小程序·https·uni-app·iphone·webview
黑科技iOS上架3 天前
ReactNative入门介绍
经验分享·ios
智塑未来3 天前
鸿蒙系统对比安卓、iOS,核心优势是什么?
android·ios·harmonyos
初雪云3 天前
iOS 上架入门:证书、描述文件、IPA 上传到底是什么关系?
android·ios·自动化·产品经理·iphone
狂热开发者3 天前
用 Typeoff 把会后口述整理成 Markdown 决策记录
人工智能·windows·macos·ios·安卓