Get System Volume iOS

后端 未结 8 2280
死守一世寂寞
死守一世寂寞 2020-11-27 05:02

My case is simple: I need to play a warning signal and want to make sure the user will hear it, so I want to check the system volume.

How can I find out what the cur

8条回答
  •  臣服心动
    2020-11-27 05:18

    Swift 2.2, make sure to import MediaPlayer

    private func setupVolumeListener()
    {
        let frameView:CGRect = CGRectMake(0, 0, 0, 0)
        let volumeView = MPVolumeView(frame: frameView)
        //self.window?.addSubview(volumeView) //use in app delegate
       self.view.addSubview(volumeView)  //use in a view controller
    
    
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(volumeChanged(_:)), name: "AVSystemController_SystemVolumeDidChangeNotification", object: nil)
    }//eom
    
    
    
    func volumeChanged(notification:NSNotification)
    {
        let volume = notification.userInfo!["AVSystemController_AudioVolumeNotificationParameter"]
        let category = notification.userInfo!["AVSystemController_AudioCategoryNotificationParameter"]
        let reason = notification.userInfo!["AVSystemController_AudioVolumeChangeReasonNotificationParameter"]
    
        print("volume:      \(volume!)")
        print("category:    \(category!)")
        print("reason:      \(reason!)")
        print("\n")
    }//eom
    

提交回复
热议问题