Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAudioSession.sharedInstance().outputVolume Not Returning Correct Volume Consistently

With the following code, I get the output volume, but it is really inconsistent - sometime it gives the same values, sometimes it is a volume change behind, despite the system volume actually changing correctly.

Any way to get this to output correct values every time?

func viewDidLoad() {
...

        NotificationCenter.default.addObserver(self, selector: #selector(volumeDidChange), name: NSNotification.Name(rawValue: "AVSystemController_SystemVolumeDidChangeNotification"), object: nil)
...

}


func volumeDidChange() {
   print("VOLUME CHANGING", AVAudioSession.sharedInstance().outputVolume)

// output while changing the volume with hardware buttons
VOLUME CHANGING 0.0625
VOLUME CHANGING 0.0625
VOLUME CHANGING 0.125
VOLUME CHANGING 0.1875
VOLUME CHANGING 0.25
VOLUME CHANGING 0.375
VOLUME CHANGING 0.375
VOLUME CHANGING 0.4375
VOLUME CHANGING 0.5
VOLUME CHANGING 0.5625
VOLUME CHANGING 0.625
VOLUME CHANGING 0.6875
VOLUME CHANGING 0.75
VOLUME CHANGING 0.8125
VOLUME CHANGING 0.875
VOLUME CHANGING 0.75
VOLUME CHANGING 0.6875
VOLUME CHANGING 0.625
VOLUME CHANGING 0.625
VOLUME CHANGING 0.5625
VOLUME CHANGING 0.5
VOLUME CHANGING 0.4375
VOLUME CHANGING 0.375
VOLUME CHANGING 0.3125
VOLUME CHANGING 0.375
VOLUME CHANGING 0.4375
VOLUME CHANGING 0.4375
like image 570
solenoid Avatar asked Jun 26 '18 03:06

solenoid


2 Answers

Try

import AVFoundation
import MediaPlayer

//MARK: Did Load
override func viewDidLoad() {
    super.viewDidLoad()

    /// Volume View
    let volumeView = MPVolumeView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
    volumeView.isHidden = false
    volumeView.alpha = 0.01
    view.addSubview(volumeView)

    /// Notification Observer
     NotificationCenter.default.addObserver(self, selector: #selector(self.volumeDidChange(notification:)), name: NSNotification.Name(rawValue: "AVSystemController_SystemVolumeDidChangeNotification"), object: nil)
}

@objc func volumeDidChange(notification: NSNotification) {
    //print("VOLUME CHANGING", AVAudioSession.sharedInstance().outputVolume)

    let volume = notification.userInfo!["AVSystemController_AudioVolumeNotificationParameter"] as! Float
    print("Device Volume:\(volume)")
}

Your current output - Same Values getting Repeated

enter image description here

Required Output

enter image description here

like image 158
iOS Geek Avatar answered Nov 20 '22 15:11

iOS Geek


Updated for Swift 4.2

override func viewDidLoad() {
    super.viewDidLoad()
    let session = AVAudioSession.sharedInstance()
    
    do {
        try session.setActive(true)
    } catch {
        print("error in getting volume")
    }
    
    if session.outputVolume < 1.0 {
        volumeButton = 0
    } else {
        volumeButton = 1
    }
    
    NotificationCenter.default.addObserver(self, selector: #selector(self.volumeDidChange(notification:)), name: NSNotification.Name(rawValue: "AVSystemController_SystemVolumeDidChangeNotification"), object: nil)
}

@objc func volumeDidChange(notification: NSNotification) {
    if let volume = notification.userInfo?["AVSystemController_AudioVolumeNotificationParameter"] as? Float{
        print("Device Volume:\(volume)")
    } else{
        print("Error while reading value...")
    }
}
like image 3
Shubham Mishra Avatar answered Nov 20 '22 13:11

Shubham Mishra