音效能為 App 增添不少臨場感,從提示音、震動回饋到自訂音效都有各自的 API。本文依序介紹 AudioToolbox、AVFoundation、MediaPlayer 等框架,並封裝一個方便重複使用的音效工具類。
首先加入 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),也不影響,只是不會震動。
對於壓縮的 Audio 檔,或超過 30 秒的音訊,可使用 AVAudioPlayer 類別。它需要知道音訊路徑,其 delegate 有兩個方法:audioDidFinishPlaying:successfully:(播放完成時觸發)與 audioPlayerEndInterruption:(被外部中斷後重新回到 App 時觸發)。
可使用 MPMoviePlayerController 播放電影檔(支援 H.264、MPEG-4 Part2 video 格式),也能播放網路上的影片檔。
實際需求大致分為三類:震動、系統音效(無需提供音訊檔)、自訂音效(需提供音訊檔)。以下是一個好用的工具類封装:
@interface WQPlaySound : NSObject
{
SystemSoundID soundID;
}
-(id)initForPlayingVibrate;
-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type;
-(id)initForPlayingSoundEffectWith:(NSString *)filename;
-(void)play;
@end
@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
步驟一:加入 AudioToolbox.framework 到專案。
步驟二:呼叫 WQPlaySound 工具類。
震動:
WQPlaySound *sound = [[WQPlaySound alloc] initForPlayingVibrate]; [sound play];
系統音效(以 Tock 為例):
WQPlaySound *sound = [[WQPlaySound alloc] initForPlayingSystemSoundEffectWith:@"Tock" ofType:@"aiff"]; [sound play];
自訂音效(把 tap.aif 加入專案):
WQPlaySound *sound = [[WQPlaySound alloc] initForPlayingSoundEffectWith:@"tap.aif"]; [sound play];
透過這個封裝,無論是震動、系統音效還是自訂音效,都能用一致的介面輕鬆呼叫,大幅簡化專案中的音效管理。