【iOS】播放按钮点击音效

有点击按钮产生音效的需求

/**
 设置签到音效
 @param name 音效名称
 @param soundtype 音效类型
 @param playtype 播放类型
 */
-(void)playSoundWithName:(NSString *)name soundtype:(NSString *)soundtype playtype:(PlaySoundType)playtype
{
    NSString *soundPath = [[NSBundle mainBundle] pathForResource:name ofType:soundtype];
    NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
    
    //生成音效
    SystemSoundID soundID = 0;
    AudioServicesCreateSystemSoundID((__bridge  CFURLRef) (soundUrl), &soundID);
    // 开始播放音效
    playtype?(AudioServicesPlayAlertSound(soundID)):(AudioServicesPlaySystemSound(soundID));
    
   /**
    //普通音效
    void AudioServicesPlaySystemSound(SystemSoundID   inSystemSoundID);
    // 开始播放音效并带震动
    void AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID);
    */
}

这是代码中的枚举类型,主要用来选择播放的方式,normal是普通播放,shake是震动

typedef NS_ENUM(NSInteger, PlaySoundType) {
    PlaySoundTypeNormal = 0,
    PlaySoundTypeShake,
};

猜你喜欢

转载自blog.csdn.net/sinat_33495069/article/details/84254388