按键音
给控件配置系统按键音非常简单,创建具有提示音功能控件的基类,然后在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地址
欢迎交流指正