按钮点击播放音效


1、引入音频管理文件

#import <AVFoundation/AVFoundation.h>

2、方法抽取

/************ 播放音效 *****************************/
- (void)playSoundEffect:(NSString *)name withType:(VoiceType)type {
	//播放扫描二维码的声音
	SystemSoundID soundID;
	NSString *strSoundFile = [[NSBundle mainBundle] pathForResource:name ofType:VoiceTypeStringMap[type]];
	AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:strSoundFile],&soundID);
	AudioServicesPlaySystemSound(soundID);
}

3、点击按钮播放音效

/************ 点击开关灯按钮就会调用 *****************************/
- (void)OpenOrClosedLamp:(UIButton *)button {
// 播放音效
[self playSoundEffect:@"noticeMusic" withType:@"wav"];
}
原文地址:https://www.cnblogs.com/CH520/p/9243679.html