繁体   English   中英

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

[英]Play a sound file when tapping table cell

当用户点击一个单元时,我想做的是我希望它播放声音。 到目前为止,我搜索过的内容必须在以下位置实现:

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

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

}

我只是不知道最好的调用方式。 任何帮助,将不胜感激。

为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];

不要忘记:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM