esq*_*qew 7 iphone audio xcode iphone-sdk-3.0
有没有人使用可用于播放短音的AudioToolBox框架的片段?如果您与我和社区其他成员分享,我将不胜感激.我看过的其他任何地方的代码似乎都不太清楚.
谢谢!
Nat*_* S. 11
以下是使用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];
}
Run Code Online (Sandbox Code Playgroud)
这假设主捆绑中有一个名为"click.caf"的文件.当我播放这个声音很多时,我实际上是把它放在后面播放而不是释放它.
我写了一个简单的Objective-C包装AudioServicesPlaySystemSound和朋友:
#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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24829 次 |
| 最近记录: |