1

I needed to write an iOS App which counts down by pressing the Up Volume Key on an iPhone or iPad. Therefore I used some advice from here to use AVAudioSession and observe the "outputVolume" key.

Here you can find the code of my ViewController:

import UIKit
import MediaPlayer

class ViewController: UIViewController, UIPopoverPresentationControllerDelegate {
    @IBOutlet weak var chickenLabel: UILabel!

    let audioSession = AVAudioSession.sharedInstance()
    var maxHendl:Int = 100
    var istHendl:Int = 100
    var isVolumeChanged = false

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillAppear(_ animated: Bool) {
        listenVolumeButton()
        removeVolumeView()
    }

    func listenVolumeButton() {
        do {
        try audioSession.setActive(true)

        }
        catch {
            print(error.localizedDescription)
        }
        audioSession.addObserver(self, forKeyPath: "outputVolume", options: .new, context: nil)
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "outputVolume" {
            if isVolumeChanged == false {
            // Set Volume to 50%
            MPVolumeView().subviews.filter{NSStringFromClass($0.classForCoder) == "MPVolumeSlider"}.first as? UISlider)?.setValue(0.5, animated: false)
                //do some Counting
            }
        }
     }
func removeVolumeView() {
    let volumeView: MPVolumeView = MPVolumeView(frame: CGRect.zero)
    view.addSubview(volumeView)
}
 }

Everything works as it should after starting the App for the first time. When I now press the Home Button and return back to the App the Events of pressing the volume buttons are not captured and the MPVolumeWindow is displayed again.

Can somebody help me to solve this issue?

Regards Armin

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Armin
  • 19
  • 2

1 Answers1

2

When you come back from being in the background, your audio session is no longer active. You need to activate it again.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I thought to do this by calling listenVolumeButton() and removeVolumeView() in ViewWillAppear. Is this not working this way? Do I have to do this in AppDelegate when DidBecomeActive is called? – Armin Mar 08 '17 at 22:35
  • `viewWillAppear` is not called just because your app becomes active after being inactive. – matt Mar 08 '17 at 23:01
  • 1
    Reactivating the AudioSession in AppDelegate (willBecomeActive) solved the problem. Thank you very much! – Armin Mar 08 '17 at 23:03
  • What all did you have to do to reactivate it in AppDelegate? Did you have to set up a notification? –  Feb 14 '19 at 17:31
  • @swiftcoder The app delegate has an activate event right there in the code. – matt Feb 14 '19 at 17:52
  • Matt, I'm confused with how to properly handle a button that becomes disabled and has it's animation frozen when the home button is pressed. I've been following this question by user SomeCleverName [https://stackoverflow.com/questions/54659670/uiview-animation-and-uibutton-stop-working-b-c-home-pressed-swift-4], because it seems very similar to my problem. Can you please elaborate more on how this is properly done. Thanks. –  Feb 14 '19 at 18:32
  • @swiftcoder sorry but that sounds like a _completely_ different matter – matt Feb 14 '19 at 19:27