码迷,mamicode.com
首页 > 其他好文 > 详细

开启声音

时间:2015-06-02 11:04:21      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

导入框架:#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(@"关闭状态————声音");
    }
}

开启声音

标签:

原文地址:http://www.cnblogs.com/conanwin/p/4545691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!