iOS 消息提醒,声音,震动

在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);

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

推荐阅读更多精彩内容

  • 以前看过一组图片,很感动,先转几张——Melting Men 我想起了雪人。 如果雪人有温度,它将耗尽最后一份热量...
    娃zi山阅读 439评论 0 2
  • 我之前有个喜欢了四年的人。阳光,帅气,学霸,是我的第一次心动。 我们的缘分应该从前后桌说起。高一,没有了初...
    aaaaaaaaaaaaa阅读 177评论 1 0
  • 夜色凝重 犹如一团化不开的忧愁 我的出现 命运注定了 一颗流星的滑过 在自己的轨道上哭泣 你有属于自己的方向 而我...
    季风无忌阅读 351评论 2 6
  • 最近并不是怎么顺利,先是衣服被划了一个洞并且我当时感觉我如果那样做,衣服会坏,可是我还是那样做了。——有的时候人...
    一减阅读 184评论 0 0