語音輸入已經是行動應用的標配能力。如果不想自己養一套語音模型,直接接入第三方服務是最現實的選擇。微信在開放平台(https://pr.weixin.qq.com/)上提供了語音與圖像相關能力,文件與 Demo 都附在 SDK 裡,依流程走即可。
一個重要前提:該 SDK 只提供真機用的 .a 靜態庫,沒有模擬器版本,因此在模擬器上執行會直接編譯或連結失敗。開發時請務必接真機。

微信語音 SDK 以 Objective-C++ 撰寫,因此有兩項設定必須處理:
libstdc++ 或 Compiler Default;Objective-C++,或者把用到 SDK 的檔案副檔名改成 .mm。
第二點最常被忽略。只要有一個檔案以 Objective-C++ 方式編譯,引入時的型別檢查就會放寬,原本在純 Objective-C 下會報的錯也就不會出現;反過來說,忘了改而直接引入,就會看到一堆語法錯誤。
#import "CustomNewViewControl.h"
#import "WXSpeechRecognizerWithUI.h"
@interface CustomNewViewControl () <WXVoiceWithUIDelegate> {
WXSpeechRecognizerWithUI *_wxssui;
__weak IBOutlet UILabel *label;
}
@property (weak, nonatomic) IBOutlet UIButton *button;
@end
@implementation CustomNewViewControl
- (void)viewDidLoad {
[super viewDidLoad];
// userKey 請替換為自己在開放平台申請到的識別碼
_wxssui = [[WXSpeechRecognizerWithUI alloc] initWithDelegate:self
andUserKey:@"你的 UserKey"];
}
- (IBAction)buttonPressed:(UIButton *)sender {
label.text = @"";
[_wxssui showAndStart]; // 彈出自帶介面並開始聆聽
}
#pragma mark - WXVoiceWithUIDelegate
- (void)voiceInputResultArray:(NSArray *)array {
WXVoiceResult *result = [array objectAtIndex:0];
[label setText:result.text];
}
@end
有 UI 的版本好處是連錄音的視覺回饋都幫你做完了,幾行就能跑起來,適合快速驗證或本身就是語音優先的應用。
接入方式與上面幾乎相同,差別在於改用 WXVoiceSDK 這個單例,並自行處理所有回呼。
但這裡有一條合規要求:無 UI 模式雖然允許你自由設計介面,卻必須在語音擷取與識別的視窗上正確、完整地標示「Powered by 微信智能」或「語音技術由微信智能提供」字樣。

#import "ViewController.h"
#import "WXVoiceSDK.h"
@interface ViewController () <WXVoiceDelegate>
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UIButton *button;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
WXVoiceSDK *speechRecognizer = [WXVoiceSDK sharedWXVoice];
speechRecognizer.silTime = 1.5f; // 可選:判定語句結束的靜音秒數
speechRecognizer.delegate = self; // 必選
[speechRecognizer setUserKey:@"你的 UserKey"];
}
#pragma mark - WXVoiceDelegate
- (void)voiceInputResultArray:(NSArray *)array {
if (array && array.count > 0) {
WXVoiceResult *result = [array objectAtIndex:0];
_label.text = result.text;
} else {
_label.text = @"";
}
}
- (void)voiceInputMakeError:(NSInteger)errorCode {
_label.text = [NSString stringWithFormat:@"錯誤:%ld", (long)errorCode];
}
- (void)voiceInputVolumn:(float)volumn {
// 可在此更新自繪的音量條動畫
}
- (void)voiceInputWaitForResult { /* 等待辨識結果 */ }
- (void)voiceInputDidCancel { /* 使用者取消 */ }
#pragma mark - 事件
- (IBAction)buttonPressed:(UIButton *)sender {
sender.selected = !sender.selected;
if (sender.selected) {
_label.text = @"錄音中...";
[[WXVoiceSDK sharedWXVoice] startOnce]; // 開始一次錄音
[_button setTitle:@"完成" forState:UIControlStateNormal];
} else {
[[WXVoiceSDK sharedWXVoice] finish]; // 結束並送出辨識
[_button setTitle:@"錄音" forState:UIControlStateNormal];
}
}
- (IBAction)cancelButtonPressed:(UIButton *)sender {
[[WXVoiceSDK sharedWXVoice] cancel]; // 放棄本次結果
[_button setTitle:@"錄音" forState:UIControlStateNormal];
}
@end
voiceInputVolumn: 等回呼都要自己接。記得保留必要的來源標示。另外三個實務提醒:startOnce 每次只跑一次識別,連續對話要重新呼叫;silTime 設太短容易把停頓當成句尾,設太長則等待感明顯,建議依實際語速調整;而 voiceInputResultArray: 回傳的是陣列,若你的場景需要多段結果,記得遍历整組而不是只取第 0 個。