我有一个错误,在音频捕获期间插入一组耳机,音频捕获仍然通过板载麦克风发生,并通过扬声器播放。我正在尝试通过音频端口捕获此音频,如果耳机已插入并正在使用中。这是我的代码的副本:
@IBAction func recordAudio(_ sender: UIButton) {
isPlayingRecording = false;
if endTime == nil {
self.startTimer()
}
recordingInProgress.text = "Recording"
recordingInProgress.isHidden = false
stopButton.isHidden = false
recordButton.isEnabled = false
setSecondaryView()
let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String
let recordingName = "\(exercise!.exerciseId).m4a"
let pathArray = [dirPath, recordingName]
filePath = NSURL.fileURL(withPathComponents: pathArray) as NSURL?
//Set up audio session
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(AVAudioSessionCategoryPlayAndRecord)
} catch _ {
}
do {
// setup how audio is going to be used (ie. audio parameters, categories, speaker)
try session.overrideOutputAudioPort(AVAudioSessionPortOverride.speaker)
} catch _ {
}
//Initialize and prepare the recorder
audioRecorder = nil
do {
let recordSettings: [String : AnyObject] = [AVFormatIDKey:Int(kAudioFormatMPEG4AAC) as AnyObject,
AVSampleRateKey:44100.0 as AnyObject,
AVNumberOfChannelsKey:1 as AnyObject,
AVEncoderAudioQualityKey:AVAudioQuality.medium.rawValue as AnyObject
]
try audioRecorder = AVAudioRecorder(url: filePath! as URL, settings:recordSettings)
} catch _ {
}
if audioRecorder != nil {
audioRecorder.delegate = self //This statement makes "RecordViewController" a delegate of "AVAudioRecorder", so that we can use "audioRecorderDidFinishRecording" function later on
audioRecorder.isMeteringEnabled = true
audioRecorder.prepareToRecord()
audioRecorder.record()
}
//Start count up timer if the exercise doesn't have a attempt time constraint.
if(exercise != nil && exercise?.skill != nil && exercise!.skill.respondtime == nil && exercise!.skill.respondtime!.intValue > 0) {
startTime = Date().timeIntervalSince1970
timerLabel.isHidden = false
timerLabel.text = readableSecondString(0.0) as String
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(RecordSoundsViewController.tick), userInfo: nil, repeats: true)
}
}
当插入一组耳机时,如何通过音频端口/耳机插孔捕获音频?我是用Swift做的。