因此,当我使用模拟器按预期工作时,我正在尝试对其进行录音和音频播放,但是在我的电话或任何电话上,我只能使用耳机听见吗?这是我的录音和播放功能 更新:我注意到手机上的重新编码不能正常工作:“端口扬声器”的选定数据源未知”
func startRecording() {
let audioFilename = getDocumentsDirectory().appendingPathComponent("recording.m4a")
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 12000,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
do {
audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
audioRecorder.delegate = self
audioRecorder.record()
} catch {
print(error.localizedDescription)
}
}
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
func playAudio(fileData:Data){
do {
audioPlayer = try AVAudioPlayer(data: fileData)
if let audioPlayer = audioPlayer {
audioPlayer.delegate = self
audioPlayer.prepareToPlay()
audioPlayer.volume = 2.0
audioPlayer.play()
}
} catch {
print(error.localizedDescription)
}
}