封装AutioTool工具类
AutioTool.h
#import <Foundation/Foundation.h>
@interface AutioTool : NSObject
+ (void)playRecoding:(NSString *)recodName;
@end
AutioTool.m
static NSMutableDictionary *_soundIDs;
+ (void)initialize
{
_soundIDs = [NSMutableDictionary dictionary];
}
+ (void)playRecoding:(NSString *)recodName{
SystemSoundID soundID = 0;
soundID = [_soundIDs[recodName] unsignedIntValue];
if (soundID == 0) {
CFURLRef url = (__bridge CFURLRef)[[NSBundle mainBundle] URLForResource:recodName withExtension:nil];
AudioServicesCreateSystemSoundID(url, &soundID);
[_soundIDs setObject:@(soundID) forKey:recodName];
}
AudioServicesPlaySystemSound(soundID);
}
VC回调
#import "ViewController.h"
#import "AutioTool.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
// 不要
- (IBAction)buyao:(id)sender {
[AutioTool playRecoding:@"buyao.wav"];
}
// 赢了
- (IBAction)win:(id)sender {
[AutioTool playRecoding:@"win.aac"];
}
//输了
- (IBAction)shule:(id)sender {
[AutioTool playRecoding:@"lose.aac"];
}