简体   繁体   English

在iPhone SDK中播放声音?

[英]Playing sounds in iPhone SDK?

Does anyone have a snippet that uses the AudioToolBox framework that can be used to play a short sound? 是否有人使用可用于播放短声音的AudioToolBox框架的片段? 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. 我看过的其他地方似乎对他们的代码不太清楚。

Thanks! 谢谢!

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. 假定主捆绑包中有一个名为“ click.caf”的文件。 As I play this sound a lot, I actually keep it around to play it later instead of releasing it. 当我播放很多声音时,实际上我会保留它以便稍后播放而不是释放它。

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 . 有关其他声音选项,请参阅此问题

Source : AudioServices - iPhone Developer Wiki 来源AudioServices-iPhone Developer Wiki

The AudioService sounds are irrespective of the the device volume controller. AudioService声音与设备音量控制器无关。 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 . 只能通过“设置” ->“ 声音” ->“ 铃声和警报”将这些声音静音。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM