将声音保持为按钮

时间:2009-10-28 11:48:14

标签: iphone

大家好,请告诉我如何将声音保持在名为“按我”的按钮

1 个答案:

答案 0 :(得分:1)

试一试:

1)将以下内容添加到视图控制器的.h文件中:

#import <AudioToolbox/AudioToolbox.h>
...
@interface MyViewController: UIViewController {
 ...
}
...    
- (IBAction)buttonPressed:(id)sender;

2)在Interface Builder中创建一个按钮

3)右键单击Interface Builder中的按钮,选择第二个选项卡,然后从“Touch Up Inside”旁边的圆圈拖动到.xib窗口中的“File's Owner”,并选择“buttonPressed”方法

4)将以下代码添加到视图控制器的.m文件中:

- (IBAction)buttonPressed:(id)sender {
  NSString *path = [[NSBundle mainBundle] pathForResource:@"mysound" ofType:@"wav"];
  SystemSoundID mySound;
  AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:path], &mySound);
  AudioServicesPlaySystemSound(mySound); 
}

5)最后确保链接到音频工具箱框架...您可以通过从项目菜单转到添加到项目...,导航到/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs来执行此操作/iPhoneSimulatorXX.sdk/System/Library/Frameworks并选择AudioToolbox.framework。确保从弹出的菜单中选择不复制和“相对于当前SDK”。

希望这有帮助!

相关问题