I'm developing a simple recording app using AVAudioSession
, AVAudioRecorder
. And I'm having trouble changing the sample rate of a ios real device.
An attempt was made to change the sample rate using the setPreferredSampleRate
function, and this attempt was well applied in all simulators. In this case, I checked that the sample rate has changed by checking the sampleRate
property of AVAudioSession
.
However, it didn't work on a real device. As a result of checking the sampleRate
property, only the value of 48000 was found in the real device. The devices I tested were iphone 8 and iphone 8+.
I googled and read a lot of stackoverflow articles, but it was hard to find an article that could be applied to my problem.
Then I found an article , I read that "The internal speaker on the iPhone 6S models only support a sample rate of 48kHz."
I wonder if there is no way to change the sample rate in a real device as long as the internal speaker is used.
If there is a way, I don't think it's just to use setPreferredSampleRate
... I'd appreciate it if you could tell me how to do it.
Below is the class I wrote to manage AVAudioSession. I changed the sample rate and checked the value using the method below.
class AudioSessionManager{
private let audioSession = AVAudioSession.sharedInstance()
func setAudioSession() {
do {
try audioSession.setCategory(.playAndRecord, options: .defaultToSpeaker)
audioSession.requestRecordPermission{ accepted in
if accepted {
print("permission granted")
}
}
} catch {
print("audioSession error: \(error.localizedDescription)")
}
}
func setSampleRate(_ sampleRate : Double) {
do {
try audioSession.setPreferredSampleRate(sampleRate)
} catch {
print("audioSession error: \(error.localizedDescription)")
}
}
func getSampleRate() -> Double {
return audioSession.sampleRate
}
}
You may possibly need to set automaticallyConfiguresApplicationAudioSession
for the session to false before your settings are applied so that they are not overridden by the system.
Related question and answer: AVCaptureSession audio samples captured at different frequency than AVAudioSession's sample rate
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.