简体   繁体   中英

How to change ios volume in swift with help UISlider

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM