How to change ios volume in swift with help UISlider

天大地大妈咪最大 提交于 2019-12-01 12:59:37

问题


I wrote the following code, but it doesn't change iOS System Volume, it only changes the sound from within the app. audioPlayer is subclass of AVAudioPlayer

   @IBAction func sliderVol(sender: UISlider) {
        audioPlayer.volume = sender.value
    }

The UISlider has the sent event of value changed
How can I change iOS system volume with help UISlider?


回答1:


The MPVolumeView class is designed to let you do exactly this. It's in MediaPlayer framework. So, add that to your app to make things build correctly.

You create it and make it visible the way you instantiate any other subclass of UIView, which you probably know by now.

You can provide your own frame and use this to change the system volume.

var wrapperView = UIView(frame: CGRectMake(30, 200, 260, 20))
self.view.backgroundColor = UIColor.clearColor()
self.view.addSubview(wrapperView)

// 3
var volumeView = MPVolumeView(frame: wrapperView.bounds)
wrapperView.addSubview(volumeView)

For more help you can look here Controlling system volume using swift



来源:https://stackoverflow.com/questions/31538921/how-to-change-ios-volume-in-swift-with-help-uislider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!