So I am trying to recorder and audio the play it back when I do that using simulator it work as expected , but on a my phone or any phone I can only hear it using headphones? this is my recording and playing functions Update : I have noticed that the recodering is not working as well on my phone :Unknown selected data source for Port Speaker"
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)
}
}