conanwin

导航

开启声音

导入框架:#import <AudioToolbox/AudioToolbox.h>
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.shake addTarget:self action:@selector(ChangeShake:) forControlEvents:UIControlEventValueChanged];
    [self.sound addTarget:self action:@selector(changeSound:) forControlEvents:UIControlEventValueChanged];
    // Do any additional setup after loading the view from its nib.
}
-(void)ChangeShake:(UISwitch *)sender{
    if (self.shake.isOn) {
        NSLog(@"开启状态————震动");
        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//系统声音
//         AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);//警告声音
    }else{
        NSLog(@"关闭状态————震动");
    }
}
-(void)changeSound:(UISwitch *)sender{
    if (self.sound.isOn) {
        //播放声音
        NSString *path = [[NSBundle mainBundle] pathForResource:@"message" ofType:@"wav"]; //音效文件路径
        SystemSoundID soundID;//组装并播放音效
        NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
        AudioServicesPlaySystemSound(soundID);
        AudioServicesDisposeSystemSoundID(soundID);//声音停止
         NSLog(@"开启状态————声音");
    }else{
        NSLog(@"关闭状态————声音");
    }
}

posted on 2015-06-02 10:20  conanwin  阅读(316)  评论(0编辑  收藏  举报