1. 程式人生 > >按鈕點擊播放音效

按鈕點擊播放音效

RR 掃描 ring oid voice notice pen res nsstring


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"];
}

按鈕點擊播放音效