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

相关推荐
麦客奥德彪30 分钟前
客户端 Trace Benchmark 体系设计
ios·数据分析
Digitally9 小时前
6 种简易方法轻松连接 iPad 与电脑
ios·电脑·ipad
2501_9159214312 小时前
uni-app一键生成iOS安装包并上传TestFlight全流程
android·ios·小程序·https·uni-app·iphone·webview
sp42a12 小时前
如何在 NativeScript 中使用 iOS 的 Metal 着色器
ios·着色器·nativescript
Mr_CrazyPeter1 天前
iONSPlayer 1.1.1版本发布
游戏·ios·模拟器
秋雨梧桐叶落莳1 天前
iOS——UI入门
ui·ios·cocoa
zhensherlock1 天前
Protocol Launcher 系列:Agenda 优雅组织你的想法与日程
javascript·macos·ios·typescript·node.js·mac·ipad
Rhys..1 天前
webdriveragent setup教程
ios
报错小能手1 天前
ios开发方向——swift内存基础
开发语言·ios·swift
SY.ZHOU1 天前
移动端架构体系(二):本地持久化与动态部署
flutter·ios·安卓