2012-01-15 36 views

回答

5

播放简短的声音,如按钮声音,其实很简单。这是一个简单的例子。

必须链接AudioToolbox.framework二进制

SoundExampleViewController.h;

#import <UIKit/UIKit.h> 
#import <AudioToolbox/AudioToolbox.h> 
@interface SoundExampleViewController : UIViewController{ 
    SystemSoundID mySoundID; 
} 
@end 

SoundExampleViewController.m:

#import "SoundExampleViewController.h" 
@implementation SoundExampleViewController 
- (void)viewDidLoad{ 
    [super viewDidLoad]; 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"SoundFileName" ofType:@"wav"]; 
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&mySoundID); 
} 
- (void)viewDidUnload{ 
    [super viewDidUnload]; 
    AudioServicesDisposeSystemSoundID(mySoundID); 
} 
@end 

然后发挥你只需拨打:AudioServicesPlaySystemSound(mySoundID);

+0

甜,非常感谢你。非常简单。 – 2012-01-15 04:40:15

+0

宏伟的答案 – Fattie 2013-12-10 12:52:56

+0

解决了我与AVAudioPlayer的内存泄漏问题,谢谢! – 2015-12-11 13:04:39

相关问题