iOS开发笔记--iphone开发震动与播放声音Demo

可能在软件某些时候需要震动手机以示提醒,可能还要播放一段特殊的声音引起用户的注意,在ios中如何实现呢?
首先实现震动,其实就是调用系统的方法,一句话就行,AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);当然前提是要加入AVFoundation.framework这个框架
然后实现播放声音,可能还要循环播放多少次,循环播放用n次,[self.player setNumberOfLoops:n];
因为我播放的声音是mp3,是用AudioToolbox框架,需要加入这个框架
代码如下

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //ViewController.h文件  
  2. #import <UIKit/UIKit.h>  
  3. #import <AVFoundation/AVFoundation.h>  
  4. #import <AudioToolbox/AudioToolbox.h>  
  5.   
  6. @interface ViewController : UIViewController{  
  7.     AVAudioPlayer *player;  
  8. }  
  9. @property (retainAVAudioPlayer *player;  
  10. @end  
  11.   
  12. //ViewController.m文件  
  13. #import "ViewController.h"  
  14.   
  15. @interface ViewController ()  
  16.   
  17. @end  
  18.   
  19. @implementation ViewController  
  20. @synthesize player;  
  21. - (BOOL) prepAudio  
  22. {  
  23.     NSError *error;  
  24.     NSString *path = [[NSBundle mainBundle] pathForResource:@"loop" ofType:@"mp3"];  
  25.     if (![[NSFileManager defaultManager] fileExistsAtPath:path]) return NO;  
  26.     self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];  
  27.     if (!self.player)  
  28.     {  
  29.         NSLog(@"Error: %@", [error localizedDescription]);  
  30.         return NO;  
  31.     }  
  32.     [self.player prepareToPlay];  
  33.     [self.player setNumberOfLoops:1];  
  34.     return YES;  
  35. }  
  36. - (void)viewDidLoad  
  37. {  
  38.     [self prepAudio];  
  39.     [self.player play];  
  40.     AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);  
  41. }  
  42. @end  

下载源代码http://www.kuaipan.cn/file/id_30491149655343777.htm

转自:http://blog.sina.com.cn/s/blog_68661bd80101d2bj.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值