Group Group Group Group Group Group Group Group Group

Hearing Test Application


I would like to implement app about hearing test as Petralex but I don’t know what topic I should learn. Any suggestion.

  • Detect headphone
  • Tone Generator for Left/Right with different frequency and amplitude
  • Detect Noise Level


Hi @ccnamtan, that’s awesome that you would like to build a hearing test app! Are you already familiar with Swift & iOS development? I’m not sure what your experience level is but I’d like to help by pointing you in the right direction.



Hi @gdelarosa, yes, I familiar with Swift and iOS development but I’m not with the audio things. Thank you in advance for pointing the direction.


Ok great! Well it appears that you’ll want to use AVFoundation. Because you haven’t worked with audio, I’m dropping this link here to get an intro to AVAudioEngine - an audio toolkit. Below that are other references. As for the detect headphones, you could try something like this…

func activateHeadPhonesStatus(){
     NotificationCenter.default.addObserver(self, selector: #selector(audioRouteChangeListener(_:)), name: AVAudioSession.routeChangeNotification, object: nil)

    @objc func audioRouteChangeListener(_ notification:Notification) {
            guard let userInfo = notification.userInfo,
                let reasonValue = userInfo[AVAudioSessionRouteChangeReasonKey] as? UInt,
                let reason = AVAudioSession.RouteChangeReason(rawValue:reasonValue) else {
            switch reason {
            case .newDeviceAvailable:
                let session = AVAudioSession.sharedInstance()
                for output in session.currentRoute.outputs where output.portType == AVAudioSession.Port.headphones {
                    headphonesConnected = true
                    print("headphone plugged in")
            case .oldDeviceUnavailable:
                if let previousRoute =
                    userInfo[AVAudioSessionRouteChangePreviousRouteKey] as? AVAudioSessionRouteDescription {
                    for output in previousRoute.outputs where output.portType == AVAudioSession.Port.headphones {
                        headphonesConnected = false
                        print("headphone pulled out")
            default: ()


You’d be using AVAudioSession. As for the detecting noise level, I would suggest checking out AudioKit - mic analysis example. AudioKit is an audio synthesis, processing, and analysis framework that has great documentation and examples. Might be worth diving into. Tone generator, I would look into AVAudioPlayerNode. Hopefully these resources help you towards your goal!



Thank you so much @gdelarosa, I tried the code but audioRouteChangeListener is not called so I searched to get some work around and I changed the code about adding observer by setting object as below.

NotificationCenter.default.addObserver(self, selector: #selector(self.audioRouteChangeListener(_:)), name: AVAudioSession.routeChangeNotification, object: AVAudioSession.sharedInstance())

I can detect the headphone now.

Next, I’ll try to create tone generator. Do you have sample code about this?


That’s awesome that you got it working for the headphones! Unfortunately I do not have any sample code for the tone generator or noise detection.



Hi @ccnamtan,
Have you tried first to play an audio file of a tone? That way you can get your app rolling.