iOS设置按键提示音,主叫提示音,来电铃声

按键音

给控件配置系统按键音非常简单,创建具有提示音功能控件的基类,然后在touch事件中调用 AudioServicesPlaySystemSound(1118)方法

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
    [super touchesBegan:touches withEvent:event];
    [self play_sound];
}

- (void)play_sound
{
    AudioServicesPlaySystemSound(1118);
}

自定义来电铃声

设置自定义播放铃声

- (void)set_music
{
    //使用自定义铃声
    NSString *path = [[NSBundle mainBundle] pathForResource:@"call_ring2"ofType:@"wav"];
    //需将音频资源copy到项目<br>
    if (path)
    {
        OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&sound);
        if (error != kAudioServicesNoError)
        {
            sound = 0;
        }
    }

}

循环播放铃声和震动

{
     AudioServicesPlaySystemSound(sound);//播放声音
    _voice_timer = [NSTimer scheduledTimerWithTimeInterval:6 target:self selector:@selector(voice) userInfo:nil repeats:YES];
    _vibrate_timer = [NSTimer scheduledTimerWithTimeInterval:1.2 target:self selector:@selector(vibrate) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop]addTimer:_voice_timer forMode:NSRunLoopCommonModes];
    [[NSRunLoop currentRunLoop]addTimer:_vibrate_timer forMode:NSRunLoopCommonModes];
}
- (void)voice
{
    AudioServicesPlaySystemSound(sound);//播放声音
}

- (void)vibrate
{
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//静音模式下震动
}

最后

demo地址
欢迎交流指正

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,880评论 25 709
  • 2. 交互(Interaction) 2.1 3D 触摸(3D Touch) 2.1.1 主屏幕交互(Home S...
    Kemr阅读 470评论 0 1
  • 雨的夜,冷静 夜的雨,疯狂 夜的苦涩浸入心肺 月无声隐没 影子挣扎着去了荒漠流浪 身躯的冷漠无语 让夜悲伤 悲...
    错过风景的季节阅读 132评论 0 4
  • 文/简娟 1、白娘子用今天的话说就是超级白富美,她长相貌美,有着1700年的修炼和各种随心所欲的功力;她背景深厚,...
    jk娟阅读 1,009评论 3 2
  • 最近出现一种情况,头脑里千言万语,每一个字眼每一句话都清清楚楚,历历在目,可一旦想要把它说出来或者写下来的时候,却...
    薄芝士阅读 221评论 0 1