Objective c 如何在iPad上播放系统按钮点击声音?
当用户在iPad上运行的应用程序中点击按钮时,我需要播放一些系统声音。如何在iOS中实现它?您应该使用AVAudioPlayer 关于使用AVAudioPlayer播放声音,有一个很棒的教程。一个非常简单的使用示例:Objective c 如何在iPad上播放系统按钮点击声音?,objective-c,ios,xcode,ipad,Objective C,Ios,Xcode,Ipad,当用户在iPad上运行的应用程序中点击按钮时,我需要播放一些系统声音。如何在iOS中实现它?您应该使用AVAudioPlayer 关于使用AVAudioPlayer播放声音,有一个很棒的教程。一个非常简单的使用示例: NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3",dataPath]; NSError *error; audioPlayer = [[AVAudioPlayer
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/audiofile.mp3",dataPath];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
如果您想播放短声音(小于30秒),可以这样轻松地播放: 注意:您必须添加AudioToolbox框架并导入它(
#导入
)
还请注意,该文件可以是:
- 持续时间不超过30秒
- 线性PCM或IMA4(IMA/ADPCM)格式
- 打包在.caf、.aif或.wav文件中
/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.sdk/System/Library/Frameworks/UIKit.framework
文件名为Tock.aiff
。只需将此文件添加到项目中即可。@LiamGeorgeBetsworth酷!我想他可以拿一份文件放进他的包里。太好了!!但是有可能播放一些系统声音吗?我不想上传任何新的想法。。。像standart一样的iphone按钮点击声音
SystemSoundID mySSID;
NSString *path = [[NSBundle mainBundle] pathForResource:@"beep" ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: path], &mySSID);
AudioServicesPlaySystemSound(mySSID);
NSString *path = [[NSBundle mainBundle] pathForResource:@"gorilla 2" ofType:@"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];