我正在尝试演奏音符,然后检测麦克风的频率。当我使用振荡器弹奏音符时,如果我将耳机连接到设备,但不能使用内置扬声器,则可以使用。当我尝试根据麦克风的输入来检测声音频率时,情况正好相反。它仅适用于内置麦克风,如果我连接了耳机,则无效。
这是我用来跟踪麦克风声音频率的方法:
private var micTrackerTimer: Timer? = nil
private var micTracker: AKMicrophoneTracker? = nil
@IBAction func listen(_ sender: UIButton) {
micTrackerTimer?.invalidate()
micTracker?.stop()
micTracker = AKMicrophoneTracker()
micTracker!.start()
DispatchQueue.main.async {
self.micTrackerTimer = Timer.scheduledTimer(
timeInterval: 0.02,
target: self,
selector: #selector(self.renderPitch),
userInfo: nil,
repeats: true
)
}
}
renderPitch
暂时仅将音高输出到UITextField
。如果我没有连接耳机,它会使用内置麦克风成功检测到频率。如果连接了耳机,则无法使用,并且以下内容会打印到Xcode控制台上:
2019-03-30 15:14:50.449326+0100 Interval Training[994:165902] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Headphones (type: Headphones)
2019-03-30 15:14:50.450672+0100 Interval Training[994:165902] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Headphones (type: Headphones)
2019-03-30 15:14:50.611809+0100 Interval Training[994:165902] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Headphones (type: Headphones)
2019-03-30 15:14:50.614450+0100 Interval Training[994:165902] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Headset Microphone (type: MicrophoneWired)
2019-03-30 15:14:50.614522+0100 Interval Training[994:165902] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Headphones (type: Headphones)
2019-03-30 15:14:50.619233+0100 Interval Training[994:165902] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Headset Microphone (type: MicrophoneWired)
2019-03-30 15:14:50.619377+0100 Interval Training[994:165902] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Headphones (type: Headphones)
2019-03-30 15:14:50.625743+0100 Interval Training[994:165902] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Headset Microphone (type: MicrophoneWired)
2019-03-30 15:14:50.625829+0100 Interval Training[994:165902] [avas] AVAudioSessionPortImpl.mm:56:ValidateRequiredFields: Unknown selected data source for Port Headphones (type: Headphones)
我使用其他动作播放声音
var player: Player!
@IBAction func play(_ sender: UIButton) {
player = Player()
player.playNote()
}
我最基本的玩家职业是这样的:
import Foundation
import AudioKit
let triangle = AKTable(.triangle, count: 256)
class Player {
var oscillator: AKOscillator!
func playNote() {
oscillator = AKOscillator(waveform: triangle)
AudioKit.output = oscillator
do {
try AudioKit.start()
} catch {
print("Unable to start AudioKit. \(error)")
}
oscillator.start()
let note: MIDINoteNumber = 57
oscillator.frequency = note.midiNoteToFrequency()
print("\(note.midiNoteToFrequency())")
oscillator.rampDuration = 0.0
oscillator.amplitude = 0.5
}
func stop() {
oscillator.stop()
do {
try AudioKit.stop()
} catch {
print("Failed to stop AudioKit...\(error)")
}
}
}
如果连接了耳机,声音将按预期开始,但是如果尝试使用内置扬声器播放声音,则不会发出声音(但仍会在控制台上打印频率)。
关于如何使它既能播放又能听的任何想法,无论我是否连接了耳机?