Technical Q&A QA1716

How do I set the volume of audio media for playback with AVPlayer on iOS?

Q:  How do I set the volume of audio media for playback with AVPlayer on iOS?

A: AVPlayer uses the system volume (controlled by the hardware volume switch) for media playback.  

Use the MPVolumeView class to present the user with a slider control in your application for setting the system audio output volume. See the MPVolumeView Class Reference for more information.

On iOS 7, you can mute the playback of audio (local or streamed) with the AVPlayer muted property:

Listing 1  Muting the playback of audio for an AVPlayer object using the muted property.

#import <AVFoundation/AVFoundation.h>
 
AVPlayer *player = <#A properly configured AVPlayer object#>;
player.muted = YES; // mute the audio

On iOS 6 and earlier, you can mute the playback of audio with AVPlayer by creating an AVAudioMix with a volume ramp to set the volume to 0 as shown in Listing 2:

Listing 2  Muting the playback of audio with AVPlayer by creating an AVAudioMix with zero volume ramp.

#import <AVFoundation/AVFoundation.h>
 
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[self myAssetURL] options:nil];
NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];
 
// Mute all the audio tracks
NSMutableArray *allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks) {
    AVMutableAudioMixInputParameters *audioInputParams =[AVMutableAudioMixInputParameters audioMixInputParameters];
    [audioInputParams setVolume:0.0 atTime:kCMTimeZero];
    [audioInputParams setTrackID:[track trackID]];
    [allAudioParams addObject:audioInputParams];
}
AVMutableAudioMix *audioZeroMix = [AVMutableAudioMix audioMix];
[audioZeroMix setInputParameters:allAudioParams];
 
// Create a player item
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
[playerItem setAudioMix:audioZeroMix]; // Mute the player item
 
// Create a new Player, and set the player to use the player item
// with the muted audio mix
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
 
// assign player object to an instance variable
self.mPlayer = player;
 
// play the muted audio
[mPlayer play];


Document Revision History


DateNotes
2014-05-21

Added information about the AVPlayer muted property for iOS 7

2010-08-27

New document that demonstrates how to set the volume of audio media for playback with AVPlayer on iOS