I'm using Xcode 10+, Swift 4, and targeting iOS 11.4+ with MPMediaPlayer
I'd like to programmatically fade out the volume of the player (like Apple does when you quit their music player). As well, I don't want to see anything (like the VolumeView display) when it functions.
It looks like all the programmatic volume controls have been deprecated.
I've read through a lot of related posts and created this function which has several problems:
@objc func fadeVolume(){
var sliderView = UISlider()
//let volumeView = MPVolumeView(frame: CGRect.init(x: self.view.frame.maxX, y: self.view.frame.maxY, width: 0, height: 0))
let volumeView = MPVolumeView(frame: .zero)
volumeView.clipsToBounds = true
volumeView.showsRouteButton = false
//volumeView.isHidden = true
volumeView.alpha = 0.001
for subview in volumeView.subviews {
if let slider = subview as? UISlider {
sliderView = slider
break
}
}
self.view.addSubview(volumeView)
volumeView.didMoveToSuperview()
let current:Float = AVAudioSession.sharedInstance().outputVolume
print("slider value: \(current)")
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
UIView.animate(withDuration: 0.4, animations: {
sliderView.setValue(0.0, animated: false)
}, completion: { finished in
// If either one of these is uncommented, the VolumeView will display.
// However, without removing from superview, the VolumeView stays hidden even when I use the phone volume buttons.
volumeView.removeFromSuperview()
//volumeView.alpha = 1.0
})
}
}
First problem: The VolumeView display shows onscreen. I've tried using different frames, and changing the alpha, etc. It's as though the animation never occurs and it goes directly to the completion block.
Second problem: The VolumeView really needs to be removed from the superview or else it will remain hidden even when I use the phone volume buttons. It should only be hidden during the execution of this function.
Third problem: The animate block does nothing. The volume will simply go from the starting point to 0 instantly - no fade.
Fourth problem: I haven't tested this but my assumption is that this won't work when the app is in the background. So the idea of using this to fade out the volume when quitting the app might not work.
Any idea how to fade out the volume without displaying anything onscreen? The answer does not necessarily need to use MPVolumeView, but I need it to work with MPMediaPlayer and when exiting the App.
from Fade (animate) Volume using MPMediaPlayer Swift 4
No comments:
Post a Comment