Class

MPVolumeView

A slider control used to set the system audio output volume, and a button for choosing the audio output route.

Overview

Use a volume view to present the user with a slider control for setting the system audio output volume, and a button for choosing the audio output route. When first displayed, the slider’s position reflects the current system audio output volume. As the user drags the slider, the changes update the volume. If the user presses the device volume buttons while sound is playing, the slider moves to reflect the new volume.

If there is an Apple TV or other AirPlay-enabled device in range, the route button allows the user to choose it. If there is only one audio output route available, the route button is not displayed.

Use this class by embedding an instance of it in your view hierarchy. The following code snippet assumes you have placed an instance of the UIView class on a view using Interface Builder, sizing and positioning it as desired to contain the volume view. Point to the UIView instance with an outlet variable—named, in the case of this example, mpVolumeViewParentView. You would typically place code like that shown in Listing 1 in your viewDidLoad method.

Listing 1

Adding a volume view to your view hierarchy

myVolumeViewParentView.backgroundColor = UIColor.clear
let myVolumeView = MPVolumeView(frame: myVolumeViewParentView.bounds)
myVolumeViewParentView.addSubview(myVolumeView)

When an audio output route that does not support volume control, such as a car head unit, is active, the volume slider is replaced with the route name.

To instead display a volume slider as an alert, use the functions described in Global Volume Setting Methods.

Customizing the Volume Slider’s Appearance

The volume slider is a UISlider object. Sliders are always displayed as horizontal bars and an indicator, or thumb, notes the current value of the slider and can be moved by the user to change the setting.

Slider controls draw the volume slider track using two distinct images, which are customizable. The region between the thumb and the end of the track associated with the slider’s minimum value is drawn using the minimum volume slider image. The region between the thumb and the end of the track associated with the slider’s maximum value is drawn using the maximum volume slider image. Assigning different images to each state lets you customize the appearance of the slider when it is enabled, disabled, highlighted, and so on.

You can also customize the volume thumb image for the slider.

Symbols

Managing Visibility of Controls

var showsVolumeSlider: Bool

A Boolean value that indicates the volume slider is visible in the volume view.

var showsRouteButton: Bool

A Boolean value that indicates whether the route button is visible in the volume view.

Customizing the Volume Slider

func maximumVolumeSliderImage(for: UIControlState)

Returns the maximum volume image associated with the specified control state.

func minimumVolumeSliderImage(for: UIControlState)

Returns the minimum volume image associated with the specified control state.

func setMaximumVolumeSliderImage(UIImage?, for: UIControlState)

Assigns a maximum volume slider image to the specified control states.

func setMinimumVolumeSliderImage(UIImage?, for: UIControlState)

Assigns a minimum volume slider image to the specified control states.

func setVolumeThumbImage(UIImage?, for: UIControlState)

Assigns a thumb image to the specified control states.

func volumeSliderRect(forBounds: CGRect)

Returns the drawing rectangle for the slider’s track.

func volumeThumbImage(for: UIControlState)

Returns the thumb image associated with the specified control state.

func volumeThumbRect(forBounds: CGRect, volumeSliderRect: CGRect, value: Float)

Returns the drawing rectangle for the volume slider’s thumb image.

var volumeWarningSliderImage: UIImage?

The image used to designate the European Union volume limit.

Customizing the Route Button

func routeButtonImage(for: UIControlState)

Returns the button image associated with the specified control state.

func routeButtonRect(forBounds: CGRect)

Returns the drawing rectangle for the route button.

func setRouteButtonImage(UIImage?, for: UIControlState)

Assigns a button image to the specified control states.

Checking for Wireless Routes

var areWirelessRoutesAvailable: Bool

A Boolean value indicating wireless routes are available.

var isWirelessRouteActive: Bool

A Boolean value that indicates whether the wireless route is active.