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;

相关推荐
VicLi5 小时前
把常用 iOS 调试操作放进一个 macOS 控制台: iCATBox
ios
茶底世界之下6 小时前
雾景需要更强反差吗?用 Awaking 检查树影与雾气的平衡
ios
终端安全笔记8 小时前
下一个大版本要动的三处:更新指令、传输要求、策略写法
网络安全·ios·1024程序员节·移动设备管理
法的空间9 小时前
AI 编程时代,为什么 Dart 分析会拖慢大型项目的开发
android·flutter·ios
season_zhu9 小时前
Skills For Real Engineers 系列之二:grill,把需求想清楚,也别把自己问进死胡同
ios·ai编程
美狐美颜SDK开放平台1 天前
准备开发直播APP?视频美颜SDK接入前的十项检查清单
android·ios·音视频·直播美颜sdk·视频美颜sdk
承渊政道1 天前
iOS远程控制电脑用什么好?iPhoneiPad远程控制Windows电脑最佳方案2026横评
windows·ios·远程控制·1024程序员节·iphone ipad
茶底世界之下1 天前
彩色建筑适合转黑白吗?用 Awaking 检查颜色消失后的层次
ios
黑科技iOS上架1 天前
App Store 26年10月新应用审核放缓大量应用不进审
ios·审核
OxYGC2 天前
手机为什么能“自动跳过“开屏广告?李跳跳、GKD 与 iOS 代理软件原理盘点
android·ios·开屏广告·广告屏蔽