//创建播放器
-(AVAudioPlayer *)audioPlayer{
// if (!_audioPlayer) {
NSError *error=nil;
NSData *data = [NSData dataWithContentsOfURL:self.savedRecordPath];
_audioPlayer = [[AVAudioPlayer alloc] initWithData:data fileTypeHint:AVFileTypeWAVE error:&error];
// _audioPlayer=[[AVAudioPlayer alloc] initWithContentsOfURL:self.savedRecordPath error:&error]; 直接使用这种方法会导致录音播放不完整
NSLog(@"创建播放器的时候拿到的录音的路径:%@", self.savedRecordPath);
_audioPlayer.numberOfLoops=0;
[_audioPlayer setVolume:1];
[_audioPlayer prepareToPlay];
_audioPlayer.delegate = self;
if (error) {
NSLog(@"创建播放器过程中发生错误,错误信息:%@",error.localizedDescription);
return nil;
}
// }
return _audioPlayer;
}
iOS开发之录音和播放问题AVAudioPlayer
最新推荐文章于 2024-10-10 12:08:33 发布
本文介绍了一个iOS应用中实现录音文件播放的方法。通过使用AVAudioPlayer类加载并播放录音文件,文章详细展示了如何从指定路径加载录音文件,并设置播放器的各项属性以确保录音能够正常播放。此外,还特别注意到了直接使用URL加载可能导致录音播放不完整的问题。
2万+

被折叠的 条评论
为什么被折叠?



