以下是一個相當基礎的 iOS 音效播放支援類別,把背景音樂與短音效的播放、音量控制、開關切換都包在一個物件裡,方便在整個 App 中重複使用。
#import <Foundation/Foundation.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
@interface Sound_Base : NSObject {
float volume;
BOOL is_turnON;
AVAudioPlayer *mainMenuMusic_Player;
SystemSoundID btn_soundID;
SystemSoundID err_soundID;
}
@property (nonatomic) BOOL is_turnON;
@property (nonatomic) float volume;
@property (nonatomic, retain) AVAudioPlayer *mainMenuMusic_player;
@property (nonatomic) SystemSoundID btn_soundID;
@property (nonatomic) SystemSoundID err_soundID;
- (void)setSoundSwitch:(BOOL)sound_state;
- (void)setUIVolume:(float)f_volume;
- (void)initSound;
- (void)playSysSound:(SystemSoundID)sys_sID;
- (void)stopAllMusic;
@end
#import "Sound_Base.h"
@implementation Sound_Base
@synthesize mainMenuMusic_player;
@synthesize volume;
@synthesize is_turnON;
@synthesize btn_soundID;
@synthesize err_soundID;
- (void)dealloc {
if (self.mainMenuMusic_player.playing) {
[self.mainMenuMusic_player stop];
}
[mainMenuMusic_player release];
if (btn_soundID) {
AudioServicesDisposeSystemSoundID(btn_soundID), btn_soundID = 0;
}
if (err_soundID) {
AudioServicesDisposeSystemSoundID(err_soundID), err_soundID = 0;
}
[super dealloc];
}
- (void)initSound {
// 背景音樂:使用 AVAudioPlayer,支援循環播放
NSString *mainMenuMusic_path = [[NSBundle mainBundle] pathForResource:@"bgm_0" ofType:@"aac"];
NSURL *mainMenuMusic_url = [NSURL fileURLWithPath:mainMenuMusic_path];
self.mainMenuMusic_player = [[AVAudioPlayer alloc] initWithContentsOfURL:mainMenuMusic_url error:nil];
self.mainMenuMusic_player.numberOfLoops = -1; // -1 代表無限循環
[self.mainMenuMusic_player prepareToPlay];
// 短音效:使用 System Sound API
NSString *btnSound_path = [[NSBundle mainBundle] pathForResource:@"sys_click" ofType:@"aiff"];
AudioServicesCreateSystemSoundID(
(CFURLRef)[NSURL fileURLWithPath:btnSound_path], &btn_soundID);
NSString *errSound_path = [[NSBundle mainBundle] pathForResource:@"sys_error" ofType:@"aiff"];
AudioServicesCreateSystemSoundID(
(CFURLRef)[NSURL fileURLWithPath:errSound_path], &err_soundID);
// TODO: 從設定中讀取音量與靜音狀態
is_turnON = YES;
float f_volume = 1.0;
[self setUIVolume:f_volume];
}
- (void)setSoundSwitch:(BOOL)sound_state {
self.is_turnON = sound_state;
if (self.is_turnON) {
self.mainMenuMusic_player.volume = self.volume;
} else {
self.mainMenuMusic_player.volume = 0;
}
}
- (void)setUIVolume:(float)f_volume {
self.volume = f_volume;
if (self.is_turnON) {
self.mainMenuMusic_player.volume = f_volume;
} else {
self.mainMenuMusic_player.volume = 0;
}
}
- (void)playSysSound:(SystemSoundID)sys_sID {
if (self.is_turnON) {
AudioServicesPlaySystemSound(sys_sID);
}
}
- (void)stopAllMusic {
[self.mainMenuMusic_player stop];
}
@end
在 App 啟動後建立這個類別的單一實例並呼叫 initSound,之後任何地方要播放音效,只要向該實例呼叫對應方法即可。新增音效時,只要在 initSound 中多建立一組 SystemSoundID 並補上 dispose 即可擴充。
作者自己也註明了幾個待補的功能:從設定檔讀取使用者保存的音量、把音量變更寫回設定、以及音效開關的持久化。目前 volume 與 is_turnON 都是寫死在 initSound 裡的初始值。
這是使用這套 API 時最常被問到的限制:AudioServicesPlaySystemSound 播放的音效音量無法由 App 控制,它直接走系統音量,只能透過 MPVolumeView 或隱私 API 去影響,兩者都不是好做法。若你需要逐音效調整音量,應該改用 AVAudioPlayer(可設 volume 屬性)或 AVAudioPlayerNode 搭配 AVAudioEngine 的 mainMixerNode。
另外提醒兩點實務細節:SystemSoundID 一定要在 dealloc 中 dispose,否則會留下系統資源;而 AudioServices 播放的格式建議用 PCM 的 CAF 或 AIFF,壓縮格式在短音效上容易有解碼延遲,造成點擊後音效慢半拍的手感問題。若要播放超過約 30 秒的內容,也必須改用 AVAudioPlayer,因為 System Sound 有長度上限。