我录制了很少的音频文件并将其保存在 NSDocumentsDirectory / Recordings 中。在tableView中我显示了音频文件名称及其持续时间。在 tableView didselectMethod 我写的这种方法播放音频文件,但它无法正常工作。我在堆栈流程上经历了很多解决方案,所有这些都采用了相同的方法。但是当我尝试相同的时候,我所需要的音频文件没有播放。最后,当我在 @interface <中声明AVAudioPlayer时,我发现了这一点。 / strong>它工作正常。是什么原因可以得到一个明确的解释。
#define recordingsFolderPath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"Recordings"]
@interface MyTableViewController ()
@end
@implementation MyTableViewController{
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableviewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse forIndexPath:indexPath];
NSError *error = nil;
NSString *path = [recordingsFolderPath stringByAppendingPathComponent:cell.textLabel.text];
NSURL *soundUrl = [NSURL fileURLWithPath:path];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
if(!audioPlayer) {
NSLog(@"Error %@ ",error.localizedDescription);
}
[audioPlayer play];
}