IOS实现静音和震动以及摇一摇功能

2016-11-03  本文已影响1386人  清风沐沐

导入头文件

#import <AudioToolbox/AudioToolbox.h>

添加一个自己的音频

AudioServicesCreateSystemSoundID(   CFURLRef                    inFileURL,
                                    SystemSoundID*              outSystemSoundID)
AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"shake_sound_male.wav" ofType:@""]]), &shakingSoundID);

替换系统的播放音频

 AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)    
AudioServicesPlaySystemSound(shakingSoundID);

实现控制消息的静音和震动功能

逻辑:
在设置页面放置:switch按钮,控制静音和震动按钮,
- 静音:播放一段没有声音的音频文件
- 震动:播放一段震动的音频文件
使用场合:

// 播放接收到新消息时的声音

- (SystemSoundID)playNewMessageSound
{
    // 要播放的音频文件地址
    NSString *audioStr = [[NSBundle mainBundle] pathForResource:@"right_answer" ofType:@"mp3"];
    NSURL *audioPath = [[NSURL alloc] initFileURLWithPath:audioStr];
    //    NSURL *bundlePath = [[NSBundle mainBundle] URLForResource:@"EaseMob" withExtension:@"bundle"];
    //    NSURL *audioPath = [[NSBundle mainBundle] pathForResource:@"right_answer" ofType:@"mp3"];
    // 创建系统声音,同时返回一个ID
    SystemSoundID soundID;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)(audioPath), &soundID);
    // Register the sound completion callback.
    AudioServicesAddSystemSoundCompletion(soundID,
                                          NULL, // uses the main run loop
                                          NULL, // uses kCFRunLoopDefaultMode
                                          EMSystemSoundFinishedPlayingCallback, // the name of our custom callback function
                                          NULL // for user data, but we don't need to do that in this case, so we just pass NULL
                                          );
    
    AudioServicesPlaySystemSound(soundID);
    
    return soundID;
}

// 震动

- (void)playVibration
{
    // Register the sound completion callback.
    AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate,
                                          NULL, // uses the main run loop
                                          NULL, // uses kCFRunLoopDefaultMode
                                          EMSystemSoundFinishedPlayingCallback, // the name of our custom callback function
                                          NULL // for user data, but we don't need to do that in this case, so we just pass NULL
                                          );
    
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}

实现摇一摇功能

[UIApplication sharedApplication].applicationSupportsShakeToEdit = YES;
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event ;
#pragma mark - Event Delegate
-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if(motion == UIEventSubtypeMotionShake) {
        // 播放声音
        AudioServicesPlaySystemSound(shakingSoundID);
//        AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
        
        // 真实一点的摇动动画
        [self shaking];
    }
}
上一篇下一篇

猜你喜欢

热点阅读