قالب وردپرس درنا توس
Home / IOS Development / Ios – Fade (animate) Volume using MPMediaPlayer Swift 4

Ios – Fade (animate) Volume using MPMediaPlayer Swift 4



I use Xcode 10+, Swift 4 and targeting iOS 11.4+
with MPMediaPlayer

I want to program the volume of the player (as Apple does when you quit the music player).
In addition, I won't see anything (like the VolumeView screen) when it works.

It seems that all programmatic volume controls have become outdated.

I've read many related posts and created this feature that has several issues:

  @objc func fadeVolume () {
var sliderView = UISlider ()
// la volumeView = MPVolumeView (frame: CGRect.init (x: self.view.frame.maxX, y: self.view.frame.maxY, width: 0, height: 0))
la volumeView = MPVolumeView (frame: .zero)
volumeView.clipsToBounds = true
volumeView.showsRouteButton = false
//volumeView.isHidden = true
volumeView.alpha = 0.001
for volumeView.subviews teaching { if the slider = teaching as? UISlider { sliderView = slider break } } self.view.addSubview (volumeView) volumeView.didMoveToSuperview () leave current: Float = AVAudioSession.sharedInstance (). output volume 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 one of these is not commented, VolumeView will display. // But without removing from monitoring, VolumeView remains hidden even when I use the phone volume buttons. volumeView.removeFromSuperview () //volumeView.alpha = 1.0 }) } }

First problem: The VolumeView screen displays on the screen. I have tried to use different frames and change the alpha, etc. It is as if the animation never occurs and it goes directly to the completion block.

Other Problem: VolumeView really needs to be removed from the monitor or else it remains hidden even when I use the phone volume buttons. It should only be hidden during the performance of this function.

Third problem: The animated block does nothing. The volume simply goes from baseline to 0 immediately – 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 you quit the app may not work.

Any idea to fade out the volume without showing anything on the screen?
The answer does not necessarily need to use MPVolumeView, but I need it to work with MPMediaPlayer and when I leave the App.


Source link