簡體   English   中英

輕按單元格時播放聲音

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM