音訊大致可以分成兩類:
(1)音效,又稱「短音訊」,播放長度通常只有 1~2 秒,在 App 裡扮演點綴角色,用來提升整體操作回饋感。
(2)音樂,例如遊戲的背景音樂,播放時間通常較長。
兩者都歸 AVFoundation.framework 管,使用這個框架時,OC 型別轉成 CoreFoundation 型別需要加上橋接。
1. 取得音效檔案路徑:
NSURL *url = [[NSBundle mainBundle] URLForResource:@"m_03.wav" withExtension:nil];
2. 載入音效檔並取得對應的 SystemSoundID:
SystemSoundID soundID = 0; AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID);
3. 播放:
AudioServicesPlaySystemSound(soundID);
要留意的是,同一個音效檔只需要載入一次,後續重複播放即可,不必每次點擊都重新建立 SoundID。
載入音效檔:
AudioServicesCreateSystemSoundID(CFURLRef inFileURL, SystemSoundID *outSystemSoundID)
釋放音效資源:
AudioServicesDisposeSystemSoundID(SystemSoundID inSystemSoundID)
播放音效:
AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)
播放音效並伴隨震動:
AudioServicesPlayAlertSound(SystemSoundID inSystemSoundID)
先匯入相依框架:

再把要播放的音效素材加入專案:

#import "YYViewController.h"
#import <AVFoundation/AVFoundation.h>
@implementation YYViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// 1. 取得音效檔完整路徑
NSURL *url = [[NSBundle mainBundle] URLForResource:@"buyao.wav" withExtension:nil];
// 2. 載入音效檔並建立 SoundID(一個 ID 對應一個音效檔)
SystemSoundID soundID = 0;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)url, &soundID);
// 需要銷毀時把 ID 傳入即可
// AudioServicesDisposeSystemSoundID(soundID);
// 3. 播放音效;PlayAlertSound 會額外觸發震動
AudioServicesPlayAlertSound(soundID);
}
@end
執行後點擊螢幕即可播放音效。
播放音樂用的是 AVAudioPlayer 類別,可以播放手機本地的音訊檔。兩個必須記住的限制:
(1)AVAudioPlayer 只能用來播放「本地」音訊。
(2)時間短的音效用 AudioServicesCreateSystemSoundID 建立,時間長的音樂則用 AVAudioPlayer。
AVAudioPlayer 依賴 AVFoundation,使用前必須匯入框架與主標頭檔:


接著把要播放的音訊檔加入專案:
#import "YYViewController.h"
#import <AVFoundation/AVFoundation.h>
@implementation YYViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// 1. 音訊檔路徑
NSURL *url = [[NSBundle mainBundle] URLForResource:@"235319.mp3" withExtension:nil];
// 2. 建立播放器(一個 AVAudioPlayer 只能播放一個 url)
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
// 3. 預先緩衝
[audioPlayer prepareToPlay];
// 4. 播放
[audioPlayer play];
}
@end
把上面這段直接跑起來,你會發現點螢幕沒聲音。原因是播放器被宣告成區域變數,方法一結束物件就被回收了。改成屬性即可:
@interface YYViewController ()
@property (nonatomic, strong) AVAudioPlayer *audioplayer;
@end
@implementation YYViewController
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSURL *url = [[NSBundle mainBundle] URLForResource:@"235319.mp3" withExtension:nil];
self.audioplayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[self.audioplayer prepareToPlay];
[self.audioplayer play];
}
@end
再次提醒:一個 AVAudioPlayer 只能播放一個 url,若要播放多個檔案,就得建立多個播放器。
在 storyboard 上擺三個按鈕,分別對應播放、暫停與停止:

#import "YYViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface YYViewController ()
@property (nonatomic, strong) AVAudioPlayer *player;
- (IBAction)play;
- (IBAction)pause;
- (IBAction)stop;
@end
@implementation YYViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSBundle mainBundle] URLForResource:@"235319.mp3" withExtension:nil];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[self.player prepareToPlay];
}
- (IBAction)play {
// 開始播放 / 繼續播放
[self.player play];
}
- (IBAction)pause {
[self.player pause];
}
- (IBAction)stop {
[self.player stop];
}
@end
這裡有個容易踩到的坑:一旦呼叫 stop,這個播放器實際上就不能再繼續使用了,如果硬要沿用,後續的播放控制會出現各種莫名其妙的問題。比較保險的寫法是搭配懶載入,stop 之後把 player 設為 nil,下次要用時再重建:
@implementation YYViewController
#pragma mark - 懶載入
- (AVAudioPlayer *)player {
if (_player == nil) {
NSURL *url = [[NSBundle mainBundle] URLForResource:@"235319.mp3" withExtension:nil];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[self.player prepareToPlay];
}
return _player;
}
- (IBAction)play {
[self.player play];
}
- (IBAction)pause {
[self.player pause];
}
- (IBAction)stop {
[self.player stop];
self.player = nil; // 停止後主動釋放,下次存取會重新建立
}
@end
按住 option 點擊 initWithContentsOfURL 看定義會發現,這個 url 是唯讀的,只能透過初始化方法設定一次:


這也意味著一個播放器物件綁一個音訊檔,天生不支援切歌。實務上的解法是封裝一個播放音樂的工具類別,內部用「音訊檔名稱 → 播放器」的字典來管理多個 AVAudioPlayer,需要哪一首就取哪一個,同時也能統一處理播放模式、音量與播放結束的代理回呼。