1. 程式人生 > >【iOS】播放按鈕點選音效

【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,
};