点击表格单元格时播放声音文件

时间:2013-07-30 02:40:05

标签: ios tableview tablecell

当用户点击我希望它播放声音的单元格时,我正在尝试做什么。到目前为止,我所搜索的内容必须在以下内容中实现:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];

}

我只是不知道调用它的最佳方法。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

为audioPlayer创建一个类变量:

AVAudioPlayer *cellTapSound;

在viewDidLoad(或viewWillAppear)中:

cellTapSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"cellTapSound" withExtension:@"mp3"] error:nil];

[cellTapSound prepareToPlay];

在didSelectRowAtIndexPath中:

// this line will rewind the player each time you tap again before it ends playing 
// you can tap as fast as you can and play some sort of a tune
// must have some fun while testing

if (cellTapSound.isPlaying) [cellTapSound setCurrentTime:0.0]; 

[cellTapSound play];

不要忘记:

  • AVFoundation.framework添加到您的项目
  • #import <AVFoundation/AVFoundation.h>
  • 将音频文件拖放到项目包中并更新AVAudioPlayer init的URL
  • 阅读AVAudioPlayer以了解您还可以用它做什么
相关问题