int systemSoundID;
AudioServicesPlaySystemS
ound(systemSoundID);
//systemSoundID的取值范围在1000-2000
//播放自己的声音,但此种播放方法主要播放一些较短的声音文件
NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath],
@"/jad0007a.wav"];
id SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
AudioServicesCreateSyste
mSoundID((CFURLRef)filePath, &soundID);
AudioServicesPlaySystemS
ound(soundID);
需求大致分为三种:
1.震动
2.系统音效(无需提供音频文件)
3.自定义音效(需提供音频文件)
我的工具类的封装:
[cpp]
//
//
WQPlaySound.h
//
WQSound
//
//
Created by 念茜 on 12-7-20.
//
Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface WQPlaySound : NSObject
{
}
-(id)initForPlayingVibrate;
-(id)initForPlayingSystemSoun
dEffectWith:(NSString *)resourceName ofType:(NSString *)type;
-(id)initForPlayingSoundEffec
tWith:(NSString *)filename;
-(void)play;
@end
[cpp]
//
//
WQPlaySound.m
//
WQSound
//
//
Created by 念茜 on 12-7-20.
//
Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "WQPlaySound.h"
@implementation WQPlaySound
-(id)initForPlayingVibrate
{
}
-(id)initForPlayingSystemSoun
dEffectWith:(NSString *)resourceName ofType:(NSString *)type
{
}
-(id)initForPlayingSoundEffec
tWith:(NSString *)filename
{
}
-(void)play
{
}
-(void)dealloc
{
}
@end
调用方法步骤:
1.加入AudioToolbox.framework到工程中
2.调用WQPlaySound工具类
2.1震动
[cpp]
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingVibrate];
[sound play];
2.2系统音效,以Tock为例
[cpp]
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSystemSoun
dEffectWith:@"Tock" ofType:@"aiff"];
[sound play];
2.3自定义音效,将tap.aif音频文件加入到工程
[cpp]
WQPlaySound *sound = [[WQPlaySound alloc]initForPlayingSoundEffec
tWith:@"tap.aif"];
[sound play];