1. 程式人生 > >ios 呼叫系統提示音教程

ios 呼叫系統提示音教程

目前做的一個專案裡用到了提示音,但是又不想新增提示音到庫裡,便開始研究呼叫系統自帶的提示音,最後終於找到了。

開始在CC上查發現好像很多人都在問,但沒人回答,我就把自己查到的東西和寫的一個demo給大家分享下吧

首先要在工程里加入Audio Toolbox framework這個庫,然後在需要呼叫的檔案裡#import <AudioToolbox/AudioToolbox.h>

最後在需要播放提示音的地方寫上
AudioServicesPlaySystemSound(1106); 
注:括號中為系統聲音的id,詳見最下面的列表。

為了方便大家測試系統聲音,我寫了一個demo供大家使用下載。

另外,如果想用自己的音訊檔案建立系統聲音來播放的同學可以參考如下程式碼。

//Get the filename of the sound file:
NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], @"/jad0007a.wav"];

//declare a system sound
id SystemSoundID soundID;

//Get a URL for the sound file
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];

//Use audio sevices to create the sound
AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
//Use audio services to play the sound
AudioServicesPlaySystemSound(soundID);

順便附上系統提示音對應的列表

AudioServices

AudioServices is a group of C functions in AudioToolbox for playing short (≤30 seconds) sounds.

Predefined sounds

There are some predefined system sounds, for the system sound ID in the range 1000 to 2000 (decimal), as shown below (from 2.0 to 5.0 beta). The system sounds are all stored in /System/Library/Audio/UISounds/.

kSystemSoundID_Vibrate. 原文來自:http://www.blogjava.net/writegull/archive/2012/04/16/374729.html