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;

相关推荐
星辰即远方13 小时前
天气预报总结
macos·ios·objective-c·cocoa
秋雨梧桐叶落莳15 小时前
计算器仿写总结
学习·macos·ios·objective-c·cocoa
末代iOS程序员华仔1 天前
iOS上架海外工具类应用合规指南:避免封号与下架风险
flutter·ios·swift
末代iOS程序员华仔1 天前
Flutter 开发 iOS App 上架全流程与常见问题分析
flutter·ios
末代iOS程序员华仔1 天前
Flutter开发iOS海外工具类应用上架合规指南与风险规避
flutter·ios
AI云海2 天前
iOS 多 Target 打包、UAT/生产测试与上架 App Store 全流程指南
ios
阿里超级工程师2 天前
最新快速申请ios打包证书和profile文件步骤
ios·打包证书
2501_915909062 天前
iOS 应用反调试技详解术 检测调试器的原理与防护实践
android·ios·小程序·https·uni-app·iphone·webview
游戏开发爱好者82 天前
TraceEagle 代理抓包详解,无感代理、去证书绑定与 HTTP/3 解密能力解析
网络协议·计算机网络·网络安全·ios·adb·https·udp
aiopencode2 天前
SwiftUI Introspect生产环境完全指南:为什么它是安全可靠的选择
后端·ios