注意:在 info.plist 中申请麦克风权限
步骤如下:
1.需要申请麦克风权限
2.调用 AVFoundation 的API
主要用到 AVAudioRecorder(录音) 、AVAudioPlayer(播放)、AVAudioSession(设置音频硬件设备)
录音开始前,先检测权限,如果允许,则设置音频硬件设备:
audioS.setCategory(AVAudioSession.Category.playAndRecord, mode: AVAudioSession.Mode.default, options: AVAudioSession.CategoryOptions.defaultToSpeaker)
audioS.setActive(true, options: AVAudioSession.SetActiveOptions.init(rawValue: 0))
设置存放路径:
letpath: String= (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?? nil)!
let order = voiceList.count as Int
let namePath = "/Record\(String(describing: order)).wav"
let voiceFilePath = path.appending(namePath)
进行录音设置:
letrecordSetting = [
AVFormatIDKey: kAudioFormatLinearPCM,
AVSampleRateKey: 8000.0,
AVNumberOfChannelsKey: 1,
AVLinearPCMBitDepthKey: 16,
AVLinearPCMIsNonInterleaved: false,
AVLinearPCMIsFloatKey: false,
AVLinearPCMIsBigEndianKey: false
] as [String : Any]
然后调用 record() 开始录音。
播放录音,只需要提供音频路径,调用 play() 即可。