0

我有一个应用程序,当在视图控制器中按下按钮时,它当前会播放 5 秒的声音。我正在尝试将此设置更改为如果我有一个表格视图,则触摸特定单元格并播放声音。我不希望您选择单元格并将其推送到另一个窗口以播放声音的动作。我只是想让声音在触摸单元格时播放。

目前对于我的按钮设置,我有以下在我的视图控制器中播放声音:

在 SoundLibViewController.h

// 识别与声音文件关联的 UIButton

-(IBAction)CarHorn:(id)sender;

@结尾

在 SoundLibViewController.m

-(IBAction)CarHorn:(id)sender {
    CFBundleRef mainBundle = CFBundleGetMainBundle();
    CFURLRef soundFileURLRef;
    soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"CarHorn", CFSTR ("wav"), NULL);

    UInt32 soundID;
    AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
    AudioServicesPlaySystemSound(soundID); 
}

不幸的是,我在网上找不到任何帮助来指出将我的代码更改为合并 tableviewcell 的方向。

举一个我想要做的例子:你进入你的 iPhone/iPad 并在设置下选择“声音”。然后您选择“铃声”,它会将您带到声音列表。如果您按下与其播放的声音相关的单元格。这就是我想做的。

4

1 回答 1

0

你需要这样的东西UITableViewDelegate

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
        NSString * soundName = [self.soundNames objectAtIndex: indexPath.row]; // Assuming you have an array of sound names

        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef soundFileURLRef;
        soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef)soundName, CFSTR ("wav"), NULL);

        UInt32 soundID;
        AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
        AudioServicesPlaySystemSound(soundID); 
}
于 2012-07-06T14:42:49.833 回答