IOS 使用系統默認聲音、震動

 

IOS成長之路-使用系統默認聲音、震動 、(真機)

導入框架:



代碼:

  1. #import <UIKit/UIKit.h>  
  2. #import <AudioToolbox/AudioToolbox.h>  
  3.   
  4. @interface MsgPlaySound : NSObject  
  5. {  
  6.     SystemSoundID sound;//系統聲音的id 取值範圍爲:1000-2000  
  7. }  
  8. - (id)initSystemShake;//系統 震動  
  9. - (id)initSystemSoundWithName:(NSString *)soundName SoundType:(NSString *)soundType;//初始化系統聲音  
  10. - (void)play;//播放  
  11. @end  

  1. #import "MsgPlaySound.h"  
  2.   
  3. @implementation MsgPlaySound  
  4.   
  5. - (id)initSystemShake  
  6. {  
  7.     self = [super init];  
  8.     if (self) {  
  9.         sound = kSystemSoundID_Vibrate;//震動  
  10.     }  
  11.     return self;  
  12. }  
  13.   
  14. - (id)initSystemSoundWithName:(NSString *)soundName SoundType:(NSString *)soundType  
  15. {  
  16.     self = [super init];  
  17.     if (self) {  
  18.         NSString *path = [NSString stringWithFormat:@"/System/Library/Audio/UISounds/%@.%@",soundName,soundType];  
  19.         //[[NSBundle bundleWithIdentifier:@"com.apple.UIKit" ]pathForResource:soundName ofType:soundType];//得到蘋果框架資源UIKit.framework ,從中取出所要播放的系統聲音的路徑  
  20.         //[[NSBundle mainBundle] URLForResource: @"tap" withExtension: @"aif"];  獲取自定義的聲音  
  21.         if (path) {  
  22.             OSStatus error = AudioServicesCreateSystemSoundID((CFURLRef)CFBridgingRetain[NSURL fileURLWithPath:path],&sound);     
  23.             if (error != kAudioServicesNoError) {//獲取的聲音的時候,出現錯誤  
  24.                 sound = nil;  
  25.             }  
  26.         }  
  27.     }  
  28.     return self;  
  29. }  
  30.   
  31. - (void)play  
  32. {  
  33.     AudioServicesPlaySystemSound(sound);  
  34. }  

apple系統默認聲音名稱說明:(此說明來自於http://bbs.weiphone.com/read-htm-tid-6262661.html

1.聲音格式是MP3或m4r的需要轉成caf格式(可先轉成aif , aiff,然後修改後綴)
2.路徑在/System/Library/Audio/UISounds 裏,需要更改的可以根據以下列表進行替換
3詳細列表:
信息 
ReceivedMessage.caf--收到信息,僅在短信界面打開時播放。 
sms-received1.caf-------三全音 
sms-received2.caf-------管鐘琴 
sms-received3.caf-------玻璃 
sms-received4.caf-------圓號 
sms-received5.caf-------鈴聲 
sms-received6.caf-------電子樂 
SentMessage.caf--------發送信息 

郵件 
mail-sent.caf----發送郵件 
new-mail.caf-----收到新郵件 

電話 
dtmf-0.caf----------撥號面板0按鍵 
dtmf-1.caf----------撥號面板1按鍵 
dtmf-2.caf----------撥號面板2按鍵 
dtmf-3.caf----------撥號面板3按鍵 
dtmf-4.caf----------撥號面板4按鍵 
dtmf-5.caf----------撥號面板5按鍵 
dtmf-6.caf----------撥號面板6按鍵 
dtmf-7.caf----------撥號面板7按鍵 
dtmf-8.caf----------撥號面板8按鍵 
dtmf-9.caf----------撥號面板9按鍵 
dtmf-pound.caf---撥號面板#按鍵 
dtmf-star.caf------撥號面板*按鍵 
Voicemail.caf-----新語音郵件 

輸入設備聲音提示 
Tock.caf-----------------------點擊鍵盤 
begin_record.caf-----------開始錄音 
begin_video_record.caf--開始錄像 
photoShutter.caf------------快門聲 
end_record.caf--------------結束錄音 
end_video_record.caf-----結束錄像 

其他 
beep-beep.caf--充電、註銷及連接電腦 
lock.caf------------鎖定手機 
shake.caf---------“這個還沒搞清楚” 
unlock.caf--------滑動解鎖 
low_power.caf--低電量提示 

語音控制 
jbl_ambiguous.caf--找到多個匹配 
jbl_begin.caf------等待用戶的輸入 
jbl_cancel.caf-----取消 
jbl_confirm.caf----執行 
jbl_no_match.caf---沒有找到匹配 

日曆 
alarm.caf--日曆提醒 

iPod Touch 1G 
sq_alarm.caf 
sq_beep-beep.caf 
sq_lock.caf 
sq_tock.caf 

更多0

發佈了25 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章