iOS 消息提醒,声音,震动

作者: Senior丶 | 来源:发表于2016-04-19 18:14 被阅读1299次

在iOS开发中,我们可能会遇到消息提醒播放声音并且震动这样的需求

  • 播放声音
// 要播放的音频文件地址
NSURL *audioPath = [[NSBundle mainBundle] URLForResource:@"sound" withExtension:@"caf"];
// 创建系统声音,同时返回一个ID
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(audioPath), &soundID);
// 播放声音后的回调 
AudioServicesAddSystemSoundCompletion(soundID,
                                          NULL, // uses the main run loop
                                          NULL, // uses kCFRunLoopDefaultMode
                                          SoundFinishedPlayingCallback, // 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);
  • 震动
// Register the sound completion callback.
AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate,
                                          NULL, // uses the main run loop
                                          NULL, // uses kCFRunLoopDefaultMode
                                          SoundFinishedPlayingCallback, // 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);

相关文章

网友评论

  • Hengry:远程推送 设置有声音,无震动。如何达到这样的效果呢?

本文标题:iOS 消息提醒,声音,震动

本文链接:https://www.haomeiwen.com/subject/cihzlttx.html