<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    張慧的博客

    張慧的博客

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      45 Posts :: 0 Stories :: 24 Comments :: 0 Trackbacks

    需求大致分為三種:
    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 <UIKit/UIKit.h>  
    10. #import <AudioToolbox/AudioToolbox.h>  
    11.   
    12. @interface WQPlaySound : NSObject  
    13. {  
    14.     SystemSoundID soundID;  
    15. }  
    16.   
    17. /** 
    18.  *  @brief  為播放震動效果初始化 
    19.  * 
    20.  *  @return self 
    21.  */  
    22. -(id)initForPlayingVibrate;  
    23.   
    24. /** 
    25.  *  @brief  為播放系統音效初始化(無需提供音頻文件) 
    26.  * 
    27.  *  @param resourceName 系統音效名稱 
    28.  *  @param type 系統音效類型 
    29.  * 
    30.  *  @return self 
    31.  */  
    32. -(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type;  
    33.   
    34. /** 
    35.  *  @brief  為播放特定的音頻文件初始化(需提供音頻文件) 
    36.  * 
    37.  *  @param filename 音頻文件名(加在工程中) 
    38.  * 
    39.  *  @return self 
    40.  */  
    41. -(id)initForPlayingSoundEffectWith:(NSString *)filename;  
    42.   
    43. /** 
    44.  *  @brief  播放音效 
    45.  */  
    46. -(void)play;  
    47.   
    48. @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];  

    tap.aif音頻文件樣例下載點擊

    posted on 2012-08-16 22:26 張慧 閱讀(7768) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 日韩免费无码一区二区三区 | 天天天欲色欲色WWW免费| 亚洲午夜无码久久久久| 一级日本高清视频免费观看| heyzo亚洲精品日韩| 成人午夜免费视频| 亚洲精品国精品久久99热| 亚洲区日韩精品中文字幕| 免费无码黄网站在线观看| 亚洲av无码成人影院一区| 国产在线观看免费不卡| 偷自拍亚洲视频在线观看99| 亚洲Aⅴ无码一区二区二三区软件 亚洲AⅤ视频一区二区三区 | 日韩在线播放全免费| 亚洲熟妇无码爱v在线观看| 国产乱子精品免费视观看片| 亚洲综合一区国产精品| 日韩午夜免费视频| 美女视频黄.免费网址| 亚洲国产综合精品中文字幕| 人妻免费一区二区三区最新| 亚洲欧洲在线观看| 无码乱肉视频免费大全合集| 亚洲人成色99999在线观看| 亚洲av日韩片在线观看| 中文字幕无线码中文字幕免费| 亚洲av无码一区二区乱子伦as| 皇色在线视频免费网站| 美女被免费网站在线视频免费| 亚洲中文字幕无码爆乳AV| 亚洲一级免费视频| 欧美亚洲精品一区二区| 国产亚洲人成网站观看| 曰批视频免费30分钟成人| 一区二区免费国产在线观看 | 老汉色老汉首页a亚洲| 天天摸夜夜摸成人免费视频| 中文字幕av无码不卡免费| 亚洲日本va在线观看| 亚洲色偷偷狠狠综合网| 免费黄色网址网站|