首先需添加 AudioToolBox 框架及要播放的聲音檔案,並在實現聲音服務的類中導入介面檔案:#import <AudioToolbox/AudioToolbox.h>
播放系統聲音主要用到 AudioServicesCreateSystemSoundID 與 AudioServicesPlaySystemSound 兩個函數,並需宣告一個 SystemSoundID 型別的變數來表示要使用的聲音檔案。
-(IBAction)playSysSound:(id)sender {
SystemSoundID soundID;
// mainBundle 回傳對應於當前程式執行檔所在目錄的 NSBundle 物件
NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"soundeffect" ofType:@"wav"];
// 一個指向檔案位置的 CFURLRef 物件與一個指向 SystemSoundID 變數的指標
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID);
AudioServicesPlaySystemSound(soundID);
}
1. 提醒音:與系統聲音的差別在於,若手機處於靜音狀態,提醒音會自動觸發震動。播放提醒音需改用 AudioServicesPlayAlertSound 而非 AudioServicesPlaySystemSound。
2. 震動:只需調用 AudioServicesPlaySystemSound() 並傳入 kSystemSoundID_Vibrate 常量即可。若設備不支援震動(如 iPad 2),也不受影響,只是不會震動。
對於壓縮的 Audio 檔案,或超過 30 秒的音頻,可使用 AVAudioPlayer 類。它同樣需要知道音頻檔案的路徑,其對應的 AVAudioPlayerDelegate 有兩個委託方法:audioDidFinishPlaying:successfully:(播放完成後觸發)與 audioPlayerEndInterruption:(被外部打斷後重新回到應用時觸發)。
可使用 MPMoviePlayerController 播放電影檔(主要支援 H.264、MPEG-4 Part2 video 格式),也能播放網際網路上的視頻檔案。
需求大致分為三類:震動、系統音效(無需提供音頻檔案)、自定義音效(需提供音頻檔案)。以下封裝一個 WQPlaySound 工具類:
WQPlaySound.h
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface WQPlaySound : NSObject
{
SystemSoundID soundID;
}
// 為播放震動效果初始化
-(id)initForPlayingVibrate;
// 為播放系統音效初始化(無需提供音頻檔案)
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type;
// 為播放特定音頻檔案初始化(需提供音頻檔案)
-(id)initForPlayingSoundEffectWith:(NSString *)filename;
// 播放音效
-(void)play;
@end
WQPlaySound.m
#import "WQPlaySound.h"
@implementation WQPlaySound
-(id)initForPlayingVibrate
{
self = [super init];
if (self) {
soundID = kSystemSoundID_Vibrate;
}
return self;
}
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type
{
self = [super init];
if (self) {
NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:resourceName ofType:type];
if (path) {
SystemSoundID theSoundID;
OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &theSoundID);
if (error == kAudioServicesNoError) {
soundID = theSoundID;
} else {
NSLog(@"Failed to create sound");
}
}
}
return self;
}
-(id)initForPlayingSoundEffectWith:(NSString *)filename
{
self = [super init];
if (self) {
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:nil];
if (fileURL != nil) {
SystemSoundID theSoundID;
OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID);
if (error == kAudioServicesNoError) {
soundID = theSoundID;
} else {
NSLog(@"Failed to create sound");
}
}
}
return self;
}
-(void)play
{
AudioServicesPlaySystemSound(soundID);
}
-(void)dealloc
{
AudioServicesDisposeSystemSoundID(soundID);
}
@end
調用步驟:
1. 將 AudioToolbox.framework 加入工程;
2. 調用 WQPlaySound 工具類:
2.1 震動:
WQPlaySound *sound = [[WQPlaySound alloc] initForPlayingVibrate];
[sound play];
2.2 系統音效(以 Tock 為例):
WQPlaySound *sound = [[WQPlaySound alloc] initForPlayingSystemSoundEffectWith:@"Tock" ofType:@"aiff"];
[sound play];
2.3 自定義音效(將 tap.aif 加入工程):
WQPlaySound *sound = [[WQPlaySound alloc] initForPlayingSoundEffectWith:@"tap.aif"];
[sound play];