[英]Play a Sound when a cell is tapped
我正在一个项目中,当按下表视图中的单元格时,将播放声音。 我将声音放入数组中,如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
//Data
soundArray = [NSArray arrayWithObjects:
[Sounds soundOfCategory:@"Sound" name:@"clipOne"],
[Sounds soundOfCategory:@"Sound" name:@"clipTwo"],
[Sounds soundOfCategory:@"Sound" name:@"clipThree"],
nil];
//Reload the table
[[self tableView] reloadData];
}
我正在使用以下播放声音:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
//Create new Sound Object
Sounds *sound = nil;
sound = [soundArray objectAtIndex:indexPath.row];
//Configure the cell
[[cell textLabel]setText:[sound name]];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
-(void)PlayClip:(NSString *)soundName
{
NSString *path = [[NSBundle mainBundle] pathForResource:soundName ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[theAudio play];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self PlayClip:[soundArray objectAtIndex:indexPath.row]];
}
但是没有运气。 我收到错误: *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[声音长度]:无法识别的选择器已发送到实例0xa35d720'
有什么问题 有更好的播放声音的方法吗? 我刚开始使用表视图,因此请原谅我的无知。
谢谢!
您在代码的最后一行有问题:
[self PlayClip:[soundArray objectAtIndex:indexPath.row]]; // PlayClip:(NSString *)
[soundArray objectAtIndex:indexPath.row]返回的Sounds
不是NSStrintg
。
我认为应该是:
[self PlayClip:[[soundArray objectAtIndex:indexPath.row] name]];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.