打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
富文本常用封装(NSAttributedString浅析)
Unity3d与iOS交互开发
Ext与后台数据库交互
ios app唤起页面跳转
C#截图操作方法大全
Visual Studio 2005 插件编程(代码行数统计插件)之一
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服