使用AVAudioRecorder录制后,AVAudioPlayer无法通过扬声器播放 [英] AVAudioPlayer NOT playing through speaker after recording with AVAudioRecorder
问题描述
我正在开发一个用于进行音频录制和所录制音频回放的应用程序.我正在使用 AVAudioSession
将类别更改为 playAndRecord
,并将其传递给 defaultToSpeaker
.
I'm working on an app that does audio recording and playback of recorded audio. I'm using AVAudioSession
to change the category to playAndRecord
as well as passing it to defaultToSpeaker
.
我的问题是,如果我启动该应用程序,播放较早的录音,它将按我希望的那样通过底部(大声)扬声器播放,如果启动该应用程序并开始记录,则可能会出现这种情况一个新的备忘然后再播放,无论我做什么,它将始终使用前置摄像头旁较安静的(电话)扬声器.我需要它从较大的扬声器(如果可能的话,实际上是所有扬声器)中播放.
My problem is, if I launch the app, play an earlier recording, it plays through the bottom (louder) speaker as I want it to and expected BUT if I launch the app and start recording a new memo then play it back, no matter what I do, it will always use the quieter (phone call) speaker that's next to front face camera. I need it to play from the louder speaker (actually all speakers if possible).
注意:我正在运行iOS 14.2.1,尽管我也尝试了其他设置,但我的手机通常设置为静音模式.我正在iPhone 12上进行测试.理想情况下,我并不是真正在寻找第三方的SDK或库解决方案,因为我希望回放既简单又原生.
NOTE: I'm running iOS 14.2.1, my phone is set to silent mode (usually) though I tried other settings as well. I'm testing on an iPhone 12. Ideally, I'm not really looking for a 3rd party SDK or library solution as I want the playback to be simple and done natively.
这是我在viewModel(从viewController初始化的)中设置audioSession的方式:
This is how I set the audioSession in my viewModel (that's initialized from a viewController):
private func setupAudioSession() {
do {
let session = AVAudioSession.sharedInstance()
try session.setCategory(.playAndRecord, options: [.defaultToSpeaker, .allowBluetooth])
try session.setActive(true)
session.requestRecordPermission({ [weak self] allowed in
guard let self = self else { return }
if allowed == false {
self.settingsAlert?()
}
})
} catch {
debugPrint("error setting up AVAudioSession")
}
}
这是我录制的方式:
func startRecording() {
guard let audioFileDir = getAudioFilePath(songId: songModel!.id) else { return }
let audioURLString = audioFileDir.absoluteString + ".m4a"
guard let audioURL = URL(string: audioURLString) else { return }
let recordingSettings: [String: Any] = [
AVFormatIDKey: Int(kAudioFormatLinearPCM),
AVLinearPCMIsNonInterleaved: false,
AVSampleRateKey: 44_100.0,
AVNumberOfChannelsKey: isStereoSupported ? 2 : 1,
AVLinearPCMBitDepthKey: 16
]
do {
audioRecorder = try AVAudioRecorder(url: audioURL, settings: recordingSettings)
audioUrl = audioURL
audioRecorder?.delegate = self
audioRecorder?.prepareToRecord()
audioRecorder?.record()
} catch {
audioRecorder = nil
debugPrint("Problem recording audio: \(error.localizedDescription)")
}
}
我什至包括了WWDC 2020的一些用于立体声录音的代码,而StereoSupported正是这样做的.
I even included some code from WWDC 2020 for stereo recording and that's what isStereoSupported does.
这就是我的播放方式
do {
if let data = songDoc?.data?.audioData {
audioPlayer = try AVAudioPlayer(data: data)
} else if let url = audioUrl {
audioPlayer = try AVAudioPlayer(contentsOf: url)
}
audioPlayer?.delegate = self
audioPlayer?.play()
isPlaying = true
} catch {
audioPlayer = nil
debugPrint("playback error: \(error.localizedDescription)")
}
我当然尝试添加以下内容:
Of course I have tried adding things like:
try session.overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
,以及每次播放或录制时都设置AVAudioSession.我尝试了一堆其他类似的方法来解决此问题,但似乎无济于事.
as well as setting the AVAudioSession each time I play or record. I've tried bunch of other similar things to fix this issue but nothing seems to work.
推荐答案
您应将音频会话模式设置为AVAudioSessionModeVideoRecording并将会话类别选项设置为AVAudioSessionCategoryOptionDefaultToSpeaker
You should set audio session mode to AVAudioSessionModeVideoRecording and set session category options to AVAudioSessionCategoryOptionDefaultToSpeaker
[会话setMode:AVAudioSessionModeVideoRecording错误:& e]
[session setMode:AVAudioSessionModeVideoRecording error:&e]
这篇关于使用AVAudioRecorder录制后,AVAudioPlayer无法通过扬声器播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!