Changing Volume Output

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.

Update: Oct 23, 2014. The rewritten version in Swift for iOS 8 and Xcode 6 is available here.

Open Xcode and create a new Single View Application. For product name, use ChangeVolumeDemo and then fill out the Organization Name, Company Identifier and Class Prefix fields with your customary values. Make sure only iPhone is selected in Devices, and that the Use Storyboards checkbox is deselected and Use Automatic Reference Counting checkbox is selected.

In The Project Settings at the Linked Frameworks and Libraries add the MediaPlayer and the AVFoundation framework.



First, Let's create our User Interface. Go to ViewController.xib. Drag a Button to the main View. The main View should look like this.

In ViewController.m in the interface section import the frameworks, declare the following property and IBAction method.

#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;

- (IBAction)playSound:(id)sender;


The audioPlayer object property is needed to load play the sound file. In the playSound method we will create the MPVolumeView object.

Go back to ViewController.xib. Select the Assistant Editor. On the right side select ViewController.m. Ctrl-click and drag from Play button to the IBAction playSound method.

Change the viewDidLoad method to

- (void)viewDidLoad 
[super viewDidLoad];

NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath = [mainBundle pathForResource:@"drum" ofType:@"mp3"];

NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSError *error = nil;

self.audioPlayer = [[AVAudioPlayer alloc] initWithData:fileData error:&error];
[self.audioPlayer prepareToPlay];
self.audioPlayer.numberOfLoops = -1;

First we load the sound file which is added to the project. Next the AVAudioPlayer object is initialized with the path to the soundfile. The numberOfLoops property of audioPlayer is negative, which means the sound will play continuously. Next, implement the playSound method.

- (IBAction)playSound:(id)sender 
[self.audioPlayer play];

UIView *wrapperView = [[UIView alloc] initWithFrame:CGRectMake(30, 200, 260, 20)]; wrapperView.backgroundColor = [UIColor clearColor];
[self.view addSubview:wrapperView];

MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame: wrapperView.bounds];
[wrapperView addSubview:volumeView];

First, we play the sound. We create a wrapper View with a clear background color. Next, we create the volume View, which will show a UISlider and a Airplay button.

Build and Run, Press the Play button and change the volume. Press the Airplay button to redirect the sound to an airplay device.



You can download the source code of the ChangeVolumeDemo at the ioscreator repository on github.