iOS 播放系统音效,自定义音效

播放系统音效、自定义音效工具类

需求大致分为三种:
1.震动
2.系统音效(无需提供音频文件)
3.自定义音效(需提供音频文件)


我的工具类的封装:

 

  1. //  
  2. //  WQPlaySound.h  
  3. //  WQSound  
  4. //  
  5. //  Created by 念茜 on 12-7-20.  
  6. //  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.  
  7. //  
  8.   
  9. #import   
  10. #import   
  11.   
  12. @interface WQPlaySound NSObject  
  13.  
  14.     SystemSoundID soundID;  
  15.  
  16.   
  17.   
  18. -(id)initForPlayingVibrate;  
  19.   
  20.   
  21. -(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type;  
  22.   
  23.   
  24. -(id)initForPlayingSoundEffectWith:(NSString *)filename;  
  25.   
  26.   
  27. -(void)play;  
  28.   
  29. @end  

 


  1. //  
  2. //  WQPlaySound.m  
  3. //  WQSound  
  4. //  
  5. //  Created by 念茜 on 12-7-20.  
  6. //  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.  
  7. //  
  8.   
  9. #import "WQPlaySound.h"  
  10.   
  11. @implementation WQPlaySound  
  12.   
  13. -(id)initForPlayingVibrate  
  14.  
  15.     self [super init];  
  16.     if (self)  
  17.         soundID kSystemSoundID_Vibrate;  
  18.      
  19.     return self;      
  20.  
  21.   
  22. -(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type  
  23.  
  24.     self [super init];  
  25.     if (self)  
  26.         NSString *path [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"pathForResource:resourceName ofType:type];  
  27.         if (path)  
  28.             SystemSoundID theSoundID;  
  29.             OSStatus error  AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &theSoundID);  
  30.             if (error == kAudioServicesNoError)  
  31.                 soundID theSoundID;  
  32.             }else  
  33.                 NSLog(@"Failed to create sound ");  
  34.              
  35.          
  36.           
  37.      
  38.     return self;  
  39.  
  40.   
  41. -(id)initForPlayingSoundEffectWith:(NSString *)filename  
  42.  
  43.     self [super init];  
  44.     if (self)  
  45.         NSURL *fileURL [[NSBundle mainBundle] URLForResource:filename withExtension:nil];  
  46.         if (fileURL != nil)  
  47.          
  48.             SystemSoundID theSoundID;  
  49.             OSStatus error AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID);  
  50.             if (error == kAudioServicesNoError){  
  51.                 soundID theSoundID;  
  52.             }else  
  53.                 NSLog(@"Failed to create sound ");  
  54.              
  55.          
  56.      
  57.     return self;  
  58.  
  59.   
  60. -(void)play  
  61.  
  62.     AudioServicesPlaySystemSound(soundID);  
  63.  
  64.   
  65. -(void)dealloc  
  66.   
  67.     AudioServicesDisposeSystemSoundID(soundID);  
  68.  
  69. @end  


调用方法步骤:
1.加入AudioToolbox.framework到工程中
2.调用WQPlaySound工具类

2.1震动

 

  1. WQPlaySound *sound [[WQPlaySound alloc]initForPlayingVibrate];  
  2. [sound play];  

 

2.2系统音效,以Tock为例

 

 

  1. WQPlaySound *sound [[WQPlaySound alloc]initForPlayingSystemSoundEffectWith:@"Tock" ofType:@"aiff"];  
  2. [sound play];  

2.3自定义音效,将tap.aif音频文件加入到工程

 

 

  1. WQPlaySound *sound [[WQPlaySound alloc]initForPlayingSoundEffectWith:@"tap.aif"];  
  2. [sound play];  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值