Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1077874
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-02-26 16:24:57

头文件:

Ios代码 复制代码 收藏代码
  1. #import
  2. #include
  3. @interface SoundViewController : 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
#import #include @interface SoundViewController : UIViewController { IBOutlet UISwitch *swcallback; IBOutlet UIPickerView *soundPicker; NSArray *soundData; SystemSoundID soundFileObject; } @property (nonatomic, retain) UISwitch *swcallback; @property (nonatomic, retain) UIPickerView *soundPicker; @property (nonatomic, retain) NSArray *soundData; @property (readonly) SystemSoundID soundFileObject; static void completionCallback (SystemSoundID mySSID, void *myself); - (IBAction) playSystemSound; - (IBAction) playAlertSound; - (IBAction) vibrate; - (IBAction) StopPlaySound; - (void) GetPlaysound; @end

实现文件:

Ios代码 复制代码 收藏代码
  1. #import "SoundViewController.h"
  2. @implementation SoundViewController
  3. @synthesize swcallback, soundPicker, soundData, soundFileObject;
  4. -(IBAction) StopPlaySound{
  5. AudioServicesRemoveSystemSoundCompletion(self.soundFileObject);
  6. }
  7. -(void) GetPlaysound{
  8. [self StopPlaySound];
  9. NSInteger row = [soundPicker selectedRowInComponent:0];
  10. NSString *soundfilename;
  11. switch (row) {
  12. case 0:
  13. soundfilename = @"1.wav";
  14. break;
  15. case 1:
  16. soundfilename = @"2.wav";
  17. break;
  18. case 2:
  19. soundfilename = @"3.wav";
  20. break;
  21. default:
  22. break;
  23. }
  24. NSString *Path = [[NSBundle mainBundle] bundlePath];
  25. NSURL *soundfileURL = [NSURL fileURLWithPath:[Path stringByAppendingPathComponent:soundfilename]];
  26. AudioServicesCreateSystemSoundID((CFURLRef)soundfileURL, &soundFileObject);
  27. if ([swcallback isOn]){
  28. AudioServicesAddSystemSoundCompletion(soundFileObject, NULL, NULL, completionCallback, (void *) self);
  29. }
  30. }
  31. static void completionCallback(SystemSoundID mySSID, void *myself){
  32. AudioServicesPlaySystemSound(mySSID);
  33. }
  34. - (IBAction) playSystemSound{
  35. [self GetPlaysound];
  36. AudioServicesPlaySystemSound(self.soundFileObject);
  37. }
  38. -(IBAction) playAlertSound{
  39. [self GetPlaysound];
  40. AudioServicesPlayAlertSound(self.soundFileObject);
  41. }
  42. - (IBAction) vibrate{
  43. AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
  44. }
  45. - (void)viewDidLoad{
  46. [super viewDidLoad];
  47. NSArray *array = [[NSArray alloc] initWithObjects:@"音效1", @"音效2", @"音效3", nil];
  48. self.soundData = array;
  49. [array release];
  50. }
  51. - (void)dealloc{
  52. [super dealloc];
  53. AudioServicesDisposeSystemSoundID(self.soundFileObject);
  54. }
  55. #pragma mark -
  56. #pragma mark soundPicker Data Soure
  57. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
  58. return 1;
  59. }
  60. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
  61. return [soundData count];
  62. }
  63. #pragma mark -
  64. #pragma mark soundPicker Delegate
  65. - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
  66. return [soundData objectAtIndex:row];
  67. }
  68. @end
#import "SoundViewController.h" @implementation SoundViewController @synthesize swcallback, soundPicker, soundData, soundFileObject; -(IBAction) StopPlaySound{ AudioServicesRemoveSystemSoundCompletion(self.soundFileObject); } -(void) GetPlaysound{ [self StopPlaySound]; NSInteger row = [soundPicker selectedRowInComponent:0]; NSString *soundfilename; switch (row) { case 0: soundfilename = @"1.wav"; break; case 1: soundfilename = @"2.wav"; break; case 2: soundfilename = @"3.wav"; break; default: break; } NSString *Path = [[NSBundle mainBundle] bundlePath]; NSURL *soundfileURL = [NSURL fileURLWithPath:[Path stringByAppendingPathComponent:soundfilename]]; AudioServicesCreateSystemSoundID((CFURLRef)soundfileURL, &soundFileObject); if ([swcallback isOn]){ AudioServicesAddSystemSoundCompletion(soundFileObject, NULL, NULL, completionCallback, (void *) self); } } static void completionCallback(SystemSoundID mySSID, void *myself){ AudioServicesPlaySystemSound(mySSID); } - (IBAction) playSystemSound{ [self GetPlaysound]; AudioServicesPlaySystemSound(self.soundFileObject); } -(IBAction) playAlertSound{ [self GetPlaysound]; AudioServicesPlayAlertSound(self.soundFileObject); } - (IBAction) vibrate{ AudioServicesPlaySystemSound(kSystemSoundID_Vibrate); } - (void)viewDidLoad{ [super viewDidLoad]; NSArray *array = [[NSArray alloc] initWithObjects:@"音效1", @"音效2", @"音效3", nil]; self.soundData = array; [array release]; } - (void)dealloc{ [super dealloc]; AudioServicesDisposeSystemSoundID(self.soundFileObject); } #pragma mark - #pragma mark soundPicker Data Soure - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return [soundData count]; } #pragma mark - #pragma mark soundPicker Delegate - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ return [soundData objectAtIndex:row]; } @end

效果图:


阅读(892) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~