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

相关推荐
鸿蒙布道师2 小时前
鸿蒙NEXT开发正则工具类RegexUtil(ArkTs)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
二流小码农4 小时前
鸿蒙开发:如何更新对象数组
android·ios·harmonyos
GeniuswongAir6 小时前
苹果新规生效:即日起不再接受iOS 17 SDK编译的应用提交
ios
恋猫de小郭1 天前
腾讯 Kuikly 正式开源,了解一下这个基于 Kotlin 的全平台框架
android·前端·ios
超人强1 天前
一文搞定App启动流程、时间监测、优化措施
ios
一牛1 天前
Appkit: 菜单是如何工作的
macos·ios·objective-c
JQShan1 天前
React Native小课堂:箭头函数 vs 普通函数,为什么你的this总迷路?
javascript·react native·ios
画个大饼1 天前
Swift与iOS内存管理机制深度剖析
开发语言·ios·swift
Ya-Jun2 天前
常用第三方库:flutter_boost混合开发
android·flutter·ios