How to change an iOS device volume programmatically?

前端 未结 4 617
天命终不由人
天命终不由人 2020-12-10 04:28

Is there a way to change the device volume programmatically? maybe using audio session?

4条回答
  •  孤城傲影
    2020-12-10 05:10

    Hacky but works (Swift 3):

    func setVolumeTo(volume: Float) {
      (MPVolumeView().subviews.filter{NSStringFromClass($0.classForCoder) == "MPVolumeSlider"}.first as? UISlider)?.setValue(volume, animated: false)
    }
    

    Don't forget to import MediaPlayer

提交回复
热议问题