Volume View Tutorial in iOS8 with Swift

When using audio in your app sometimes you want the user to change the volume. With the help of the MPVolumeView object you create a builtin Volume control which also has the abilty  to redirect the output to an airplay device. In this tutorial we will play a sound and display the volume controls. This tutorial is built with iOS 8.1 and Xcode 6.1

Open Xcode and create a new Single View Application. For product name, use iOS8SwiftVolumeTutorial and then fill out the Organization Name and Organization Identifier with your customary values. Select Swift as Language and make sure iPhone is selected in Devices.

We need some audio to play, so download the following music file and add it to the project, make sure "Copy items if needed" is selected

Go to the Storyboard, drag a Button from the Object Library to the View Controller inside the Storyboard. Double-click the Button and give it a title of "Play". Select the Button and ctrl - drag to the top of the view Controller, hold down ctrl and select "Top Space to Top Layout Guide" and "Center Horizontally in Container". This will center the button and it will be pinned to the top of the view.

Open the Assistant Editor and make sure the ViewController.swift file is visible. Ctrl + drag from the Button to the ViewController class and create the following Action

We need the AVFoundation framework for playing the music file and the MediaPlayer framework to use the MPVolumeView. Go to the ViewController.swift file and import the frameworks

import AVFoundation
import MediaPlayer

Inside the ViewController class add  a property of type AVAudioPlayer.

var audioPlayer = AVAudioPlayer()

Change the viewDidLoad method to

override func viewDidLoad() {
    // 1
    let path = NSBundle.mainBundle().pathForResource("Amorphis - My Enemy", ofType: "mp3")
    var music = NSURL(fileURLWithPath: path!)
    // 2
    AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
    AVAudioSession.sharedInstance().setActive(true, error: nil)
    // 3
    var error:NSError?
    audioPlayer = AVAudioPlayer(contentsOfURL: music, error: &error)
  1. The audio file is extracted from the Application's bundle
  2. The audio session category is set to playback and set to active
  3. The audio player is initialized for playing an audio file

Implement the playSound method

@IBAction func playSound(sender: AnyObject) {
    // 1
    // 2
    var wrapperView = UIView(frame: CGRectMake(30, 200, 260, 20))
    self.view.backgroundColor = UIColor.clearColor()
    // 3
    var volumeView = MPVolumeView(frame: wrapperView.bounds)
  1. The audio file starts to play
  2. A wrapper View is created and added as a subview to the main view
  3. The MPVolumeView is added as a subview of the wrapper view.

Build and Run the project, select the play button and change the volume and Output source. Note this can only be run on an actual device, since in the iOS Simulator you will only see a black view.

You can download the source code of the iOS8SwiftVolumeTutorial at the ioscreator repository on Github.