ios中播放声音和震动

Objective-c代码   收藏代码
  1. //  
  2. //  AudioUtil.m  
  3. //  iaiai  
  4. // QQ:176291935  
  5. //  
  6. //  Created by iaiai on 14-9-25.  
  7. //  Copyright (c) 2014年 boying. All rights reserved.  
  8. //  
  9.   
  10. #import "AudioUtil.h"  
  11. #import <AudioToolbox/AudioToolbox.h>  
  12.   
  13. @implementation AudioUtil  
  14.   
  15. int number = 0;  
  16.   
  17. /**  
  18.   * 播放声音  
  19.   * @param name 资源名称  
  20.   * @param ext 资源后缀名  
  21.  **/  
  22. -(void)audioPlay:(NSString*)name ext:(NSString*)ext{  
  23.     [self audioPlay:name ext:ext num:1];  
  24. }  
  25.   
  26. /**  
  27.  * 播放声音  
  28.  * @param name 资源名称  
  29.  * @param ext 资源后缀名  
  30.  * @param num 循环次数  
  31.  **/  
  32. -(void)audioPlay:(NSString*)name ext:(NSString*)ext num:(NSInteger*)num{  
  33.     NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:ext];  
  34.     SystemSoundID soundID;  
  35.     AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &soundID);  
  36.     AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, audioPlayFinish,num);  
  37.     AudioServicesPlaySystemSound(soundID);  
  38.     number++;  
  39. }  
  40.   
  41. /**  
  42.  * 震动  
  43.  **/  
  44. -(void)vibratePlay{  
  45.     [self vibratePlay:1];  
  46. }  
  47.   
  48. /**  
  49.  * 震动  
  50.  * @param num 震动次数  
  51.  **/  
  52. -(void)vibratePlay:(NSInteger*)num{  
  53.     AudioServicesAddSystemSoundCompletion(kSystemSoundID_Vibrate, NULL, NULL, audioPlayFinish,num);  
  54.     AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);  
  55.     number++;  
  56. }  
  57.   
  58. /**  
  59.   * 播放完成之后的回调方法  
  60.   * @param soundID 播放的声音ID  
  61.   * @param num 播放次数  
  62.  **/  
  63. void audioPlayFinish(SystemSoundID soundID,NSInteger* num){  
  64.     if (num&&num>0) {  
  65.         if (number>=num) {  
  66.             if (soundID!=kSystemSoundID_Vibrate) {  
  67.                 AudioServicesDisposeSystemSoundID(soundID);  
  68.             }  
  69.         }else{  
  70.             AudioServicesPlaySystemSound(soundID);  
  71.             number++;  
  72.         }  
  73.     }else{  
  74.         if (soundID!=kSystemSoundID_Vibrate) {  
  75.             AudioServicesDisposeSystemSoundID(soundID);  
  76.         }  
  77.     }  
  78. }  
  79.   
  80. @end  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值