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;

相关推荐
末代iOS程序员华仔3 小时前
iOS 语聊直播/聊天APP4.3条例被拒解决
flutter·ios·swift
2601_965798471 天前
Build a Fast, High-Ranking Restaurant Website with Rolanda Theme
开发语言·ios·swift
极客互动API1 天前
企业微信 iPad 协议消息接口开发:文本 / 图片 / 群发消息的统一封装
人工智能·ios·微信·机器人·企业微信·ipad
我血条子呢1 天前
iOS 软键盘遮挡底部输入框解决报告
ios
思盛iOS签名上架1 天前
为什么 iOS 需要签名?未签名 App 无法安装的底层逻辑
ios
Data_Journal2 天前
Golang 中解析 HTML 的指南
ios·iphone
jike_20262 天前
销售拜访记录APP推荐:客户沟通内容怎么快速整理?
ios·语音识别·iphone
00后程序员张2 天前
iOS 开发全流程需要哪些工具?编码、调试、构建、发布阶段选择
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
游戏开发爱好者83 天前
WebSocket 抓包怎么查看内容?从握手到消息帧完整解读
网络协议·计算机网络·网络安全·ios·adb·https·udp