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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值