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

导入头文件

#import 

添加一个自己的音频

  • inFileURL : 音频文件路径
  • outSystemSoundID:该音频标记的ID,为后面播放此音频时的查找
AudioServicesCreateSystemSoundID(   CFURLRef                    inFileURL,
                                    SystemSoundID*              outSystemSoundID)
  • 实现案列
AudioServicesCreateSystemSoundID((__bridge CFURLRef)([NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"shake_sound_male.wav" ofType:@""]]), &shakingSoundID);

替换系统的播放音频

  • inSystemSoundID : 添加音频到系统时标记的ID
 AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)    
  • 播放上面的一段音频文件
AudioServicesPlaySystemSound(shakingSoundID);

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

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

  • 聊天消息:
  • 本地通知和APNS:

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

- (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);
}

实现摇一摇功能

  • 设置app是否支持震动功能
[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];
    }
}

你可能感兴趣的:(IOS实现静音和震动以及摇一摇功能)