Audio control on iOS
Users should be able to control audio whenever it plays automatically. This includes being able to reduce the volume to zero. It's difficult to hear speech output of the screen reader users when other audio is playing at the same time.
In iOS apps you should always provide a pause or stop button for media. When using AVPlayer
, you should use play
and pause
methods.
You should also make sure that audio is played through the correct channel. Use AVAudioSession
in combination with AVAudioSessionCategory
to achieve this.
The following channels are available:
AVAudioSessionCategoryAmbient
: use this channel if the sound is not important for the functioning of the appAVAudioSessionCategoryMultiRoute
: use this channel if you are sending the sound to multiple output devices at the same timeAVAudioSessionCategoryPlayAndRecord
: use this channel for sound recording and playbackAVAudioSessionCategoryPlayback
: use this channel to play recorded music and other sounds that are important for the app's functioningAVAudioSessionCategoryRecord
: use this channel for sound recording; other sound is mutedAVAudioSessionCategorySoloAmbient
: the default channel to play sound
// Set audio channel
try AVAudioSession.sharedInstance().setCategory(
.playback,
mode: .default,
options: []
)
// Provide media controls
@objc private func click(_ sender: UIButton) {
if player.timeControlStatus == .playing {
player.pause()
} else {
player.play()
}
}