xcode 在 iPhone SDK 中播放声音?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2483470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Playing sounds in iPhone SDK?
提问by esqew
Does anyone have a snippet that uses the AudioToolBox framework that can be used to play a short sound? I would be grateful if you shared it with me and the rest of the community. Everywhere else I have looked doesn't seem to be too clear with their code.
有没有人有一个使用 AudioToolBox 框架的片段,可以用来播放短声音?如果您与我和社区的其他人分享,我将不胜感激。我看过的其他地方似乎都不太清楚他们的代码。
Thanks!
谢谢!
回答by Nathan S.
Here is an easy example using the AVAudioPlayer:
这是一个使用 AVAudioPlayer 的简单示例:
-(void)PlayClick
{
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"click"
ofType:@"caf"]];
AVAudioPlayer *click = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[click play];
[click release];
}
This assumes a file called "click.caf" available in the main bundle. As I play this sound a lot, I actually keep it around to play it later instead of releasing it.
这假设主包中有一个名为“click.caf”的文件。当我经常播放这种声音时,我实际上会保留它以便稍后播放而不是释放它。
回答by zoul
I wrote a simple Objective-C wrapper around AudioServicesPlaySystemSound
and friends:
我AudioServicesPlaySystemSound
和朋友们写了一个简单的 Objective-C 包装器:
#import <AudioToolbox/AudioToolbox.h>
/*
Trivial wrapper around system sound as provided
by Audio Services. Don't forget to add the Audio
Toolbox framework.
*/
@interface Sound : NSObject
{
SystemSoundID handle;
}
// Path is relative to the resources dir.
- (id) initWithPath: (NSString*) path;
- (void) play;
@end
@implementation Sound
- (id) initWithPath: (NSString*) path
{
[super init];
NSString *resourceDir = [[NSBundle mainBundle] resourcePath];
NSString *fullPath = [resourceDir stringByAppendingPathComponent:path];
NSURL *url = [NSURL fileURLWithPath:fullPath];
OSStatus errcode = AudioServicesCreateSystemSoundID((CFURLRef) url, &handle);
NSAssert1(errcode == 0, @"Failed to load sound: %@", path);
return self;
}
- (void) dealloc
{
AudioServicesDisposeSystemSoundID(handle);
[super dealloc];
}
- (void) play
{
AudioServicesPlaySystemSound(handle);
}
@end
Lives here. For other sound options see this question.
回答by Govind
Source: AudioServices - iPhone Developer Wiki
来源:AudioServices - iPhone 开发者维基
The AudioService sounds are irrespective of the the device volume controller. Even if the phone is in silent mode the Audio service sounds will be played. These sounds can only be muted by going Settings-> Sounds-> Ringer and Alerts.
AudioService 声音与设备音量控制器无关。即使手机处于静音模式,也会播放音频服务声音。这些声音只能通过Settings-> Sounds-> Ringer and Alerts静音。
Custom system sounds can be played by the following code:
自定义系统声音可以通过以下代码播放:
CFBundleRef mainbundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef = CFBundleCopyResourceURL(mainbundle, CFSTR("tap"), CFSTR("aif"), NULL);
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundFileObject);
Invoking the vibration in iPhone
在 iPhone 中调用振动
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
Playing inbuilt system sounds.
播放内置系统声音。
AudioServicesPlaySystemSound(1100);