此特定功能的目的是能够从表格视图中显示和播放本地录音。加载本地caf文件的代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadFileInformation];
}
- (void)loadFileInformation
{
items = [[NSMutableArray alloc] init];
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
for (int i = 0; i < [directoryContent count]; i++) [items addObject:[directoryContent objectAtIndex:i]];
[self.tableView reloadData];
}
使用委托方法播放特定文件如下:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[items objectAtIndex:indexPath.row] filePathURL] error:nil];
avAudioPlayer.delegate = (id)self;
[avAudioPlayer prepareToPlay];
[avAudioPlayer play];
}
但是,当调用此方法时,出现错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString filePathURL]: unrecognized selector sent to instance 0x239420'
我只是想知道是否有人可以指出我正确的方向!当本地目录的内容存储在数组中directoryContent
时,每个元素是什么对象?NSData
?
提前谢谢了