iOS 手机铃声+连续震动效果的实现

"从一个菜鸟演变成一个老鸟这个过程和漫长,只有在不断的碰磕中你才会成长,我已经遍体鳞伤了."就此打住,不要再发那些无用的感慨了,谁都知道有这么一回事啦!

废话不多说了直接切入主题首先你要把自己的项目完成基本的推送功能,大家可以参考这两篇文章:点我我就告诉你,反正你不点我我是不会告诉你的,实现基本的推送功能后,把你的声音文件拖入工程,选copy items if needed.

然后告诉后台把链接apns发送的那个字典

{ "aps":  {

   "alert": {

   "title":"Realtime Custom Push Notifications",

   "subtitle":"Now with iOS 10 support!",

   "body":"Add multimedia content to you notifications"  

              },

   "sound":"default",

    "badge": 1,

    "mutable-content": 1,  

    "category": "realtime",

    },

   "media":   {"type":"image","url":"https://www.fotor.com/images2/features/photo_effects/e_bw.jpg"}

}  

 中的 "sound":"default"中的default换成你的音乐文件 

 "sound":"myMusic.mp3"即可,是不是炒鸡很简单?

 当然你也可以用点我我帮你测试,是一个mac软件很不错。

完成上面那些操作铃声就配置的七七八八啦!

当然也可以自定义铃声其实也很简单的,首先导入

#import

@interface myMusicViewControll()

@property(nonatomic,strong)AVAudioPlayer *audioplayer;

@end

@implementation myMusicViewControll

//1.音频文件的url路径

NSURL *url=[[NSBundle mainBundle]URLForResource:@"myMusic.mp3" withExtension:Nil];

//2.创建播放器(注意:一个AVAudioPlayer只能播放一个url)

self.audioplayer =[[AVAudioPlayer alloc]initWithContentsOfURL:url error:Nil]; 

//3.缓冲

[self.audioplayer prepareToPlay]; 

//4.播放

[self.audioplayer play];  

//5.停止铃声 

 [self.audioplayer stop];  

@end

铃声响! 响! 响起来!

接下来就一起玩一下连续震动的效果吧!其实震动代码一行就可以搞定了:AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);  //震动 ,但是就只能震动一下

@interface myMusicViewControll()

@property (nonatomic,strong)NSTimer *_vibrationTimer;  //振动计时器

@end

@implementation myMusicViewControll

@synthesize _vibrationTimer;

_vibrationTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(playkSystemSound) userInfo:nil repeats:YES];

//  振动                                                                                                                                                                                                                                                   - (void)playkSystemSound {                                                                                                                                                                                                                                                                                                                                                                AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);                                                                                                                                                                                                                                           }

// 停止震动                                                                                                                                                                                               AudioServicesRemoveSystemSoundCompletion(kSystemSoundID_Vibrate);

@end

以上是我最近搞一个项目所做的一些效果,有问题的可以留言,写的不对的地方也希望帮忙指出来,大家可以一起讨论一下,微信当App运行到后台,还能连续震动研究了很久还没有结果,有会的可以指导一下.~~~

你可能感兴趣的:(iOS 手机铃声+连续震动效果的实现)