您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iPhone 錄音指南:AVAudioRecorder 的基本用法

編輯:iOS 開發綜合

錄音前的準備

使用 AVAudioRecorder 前,要先引入 AVFoundation。錄音檔通常會存到 Documents 目錄,因此可先取得 Documents 路徑,再組出完整檔案路徑。

NSString *destinationString = [[self documentsPath] stringByAppendingPathComponent:filenameField.text];
NSURL *destinationURL = [NSURL fileURLWithPath:destinationString];
NSError *recorderSetupError = nil;
AVAudioRecorder *audioRecorder = [[AVAudioRecorder alloc] initWithURL:destinationURL settings:recordSettings error:NULL];

錄音參數

settings 是包含鍵值對的 NSDictionary。線性 PCM 常見設定包括:

  • AVFormatIDKey:格式識別碼,例如 kAudioFormatLinearPCM。
  • AVSampleRateKey:取樣率。
  • AVNumberOfChannelsKey:聲道數,1 代表單聲道,2 代表立體聲。
  • AVLinearPCMBitDepthKey:取樣位元深度,常見為 16。
  • AVLinearPCMIsBigEndianKey:是否使用大端序。
  • AVLinearPCMIsFloatKey:取樣資料是整數或浮點數。

建立 AVAudioRecorder 後,錄音流程就很直接:

[audioRecorder record];
[audioRecorder pause];
[audioRecorder stop];

實務上也要處理錄音權限、儲存路徑與檔案名稱衝突等問題,才能做出穩定的錄音功能。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved