0

ユーザーがセルをタップしたときにやろうとしていること 音を鳴らしたい。私が検索したものとはかけ離れており、次の場所に実装する必要があります。

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

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

}

私はそれを呼び出す最良の方法を知りません。どんな助けでも大歓迎です。

4

1 に答える 1

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について読んで、他に何ができるかを調べてください。
于 2013-07-30T02:54:00.437 に答える