代码改变世界

SOUND SERVICE系统音效服务+震动功能实现

2012-03-21 13:57  java环境变量  阅读(376)  评论(0编辑  收藏  举报
5.jpg 
2010-9-26 22:19:18 上传
下载附件 (42.9 KB)


PlaySoundDemo1ViewController.h
  1. #import <UIKit/UIKit.h>
  2. #include <AudioToolbox/AudioToolbox.h>

  3. @interface PlaySoundDemo1ViewController : UIViewController {
  4.     IBOutlet UISwitch *swcallback;
  5.     IBOutlet UIPickerView *soundPicker;
  6.     NSArray *soundData;
  7.         SystemSoundID        soundFileObject;   
  8. }

  9. @property (nonatomic,retain) UISwitch *swcallback;
  10. @property (nonatomic,retain) UIPickerView *soundPicker;
  11. @property (nonatomic,retain)  NSArray *soundData;
  12. @property (readonly)        SystemSoundID        soundFileObject;

  13. static void completionCallback (SystemSoundID  mySSID, void* myself) ;
  14. - (IBAction) playSystemSound;
  15. - (IBAction) playAlertSound;
  16. - (IBAction) vibrate;
  17. -(IBAction) stopplaysound;
  18. -(void) GetPlaysound;

  19. @end
复制代码
PlaySoundDemo1ViewController.m
  1. //----------- 限制 ------------
  2. //声音长度要小于 30 秒
  3. // In linear PCM 或者 IMA4 (IMA/ADPCM) 格式的
  4. //需为 .caf, .aif, 或者 .wav 的档案
  5. //不能控制播放的进度
  6. //调用方法后立即播放声音
  7. //没有循环播放和立体声控制
  8. //参考数据:
  9. //http://blog.xiang.li/2009/02/iphone-dev-audio-play/
  10. //https://developer.apple.com/iphone/library/samplecode/SysSound/

  11. #import "PlaySoundDemo1ViewController.h"

  12. @implementation PlaySoundDemo1ViewController

  13. @synthesize swcallback,soundPicker,soundData,soundFileObject;

  14. //停止当前音效的播放
  15. -(IBAction) stopplaysound{    
  16.     AudioServicesRemoveSystemSoundCompletion (self.soundFileObject);
  17. }

  18. //建立声音对象
  19. -(void) GetPlaysound{
  20.     [self stopplaysound];
  21.     
  22.    //取出使用者所选择的音效项目 
  23.    NSInteger row=[ soundPicker selectedRowInComponent:0];
  24.    
  25.     //指定不同的音效文件
  26.     NSString *soundfilename;
  27.     switch (row) {
  28.         case 0:
  29.              soundfilename=@"sound1.aiff"; 
  30.             break;
  31.         case 1:
  32.              soundfilename=@"sound2.aiff"; 
  33.             break;
  34.         case 2:
  35.              soundfilename=@"sound3.aiff"; 
  36.             break;            
  37.         default:
  38.             break;
  39.     }

  40.    
  41.     NSString *Path=[[NSBundle mainBundle] bundlePath];
  42.     NSURL *soundfileURL=[NSURL fileURLWithPath:[Path stringByAppendingPathComponent:soundfilename]]; 
  43.     
  44.     //建立音效对象
  45.     AudioServicesCreateSystemSoundID((CFURLRef)soundfileURL, &soundFileObject);
  46.     
  47.     //判断是否连续播放
  48.     if ([swcallback isOn]){
  49.         // Add sound completion callback
  50.         AudioServicesAddSystemSoundCompletion (soundFileObject, NULL, NULL,completionCallback,(void*) self);    
  51.     }    
  52. }

  53. //当音效播放完毕后的处理方式,这里设定为再一次播放
  54. static void completionCallback (SystemSoundID  mySSID, void* myself) {
  55.     AudioServicesPlaySystemSound(mySSID);    


  56. - (IBAction) playSystemSound{     
  57.      [self GetPlaysound];
  58.         AudioServicesPlaySystemSound (self.soundFileObject);    
  59.   //  AudioServicesDisposeSystemSoundID (self.soundFileObject);
  60. }

  61. -(IBAction) playAlertSound{   
  62.      [self GetPlaysound];
  63.     //播放音效
  64.     AudioServicesPlayAlertSound (self.soundFileObject);    
  65. }
  66. - (IBAction) vibrate{
  67.     //震动
  68.         AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
  69. }

  70. - (void)viewDidLoad {
  71.     [super viewDidLoad];
  72.     
  73.     //建立音效清单数组
  74.     NSArray *array=[[NSArray alloc] initWithObjects:@"音效1",@"音效2",@"音效3",nil];
  75.     self.soundData=array;
  76.     [array release];
  77. }
  78. - (void)dealloc {
  79.     [super dealloc];
  80.     AudioServicesDisposeSystemSoundID (self.soundFileObject);
  81. }
  82. #pragma mark -
  83. #pragma mark soundPicker Data Soure 
  84. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
  85.     return 1;
  86. }
  87. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
  88.     return [soundData count];
  89.   }
  90. #pragma mark -
  91. #pragma mark soundPicker Delegate 
  92. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
  93.     return [soundData objectAtIndex:row];
  94. }