Iphone – AVPlayer Volume Control

avaudioplayeravplayeriphonevolume

I want to create a button that mutes the audio from an AVPlayer.

Why canĀ“t I use .volume with AVPlayer, only with AVAudioPlayer?
Is there another way to change the volume?

e.g music.volume = 0.0;

Thanks for your answers.

Best Answer

Starting in iOS 7, simply call:

myAvPlayer.volume = 0;

Otherwise, you'll have to use the annoying setAudioMix solution. I'm detecting support for this in my app as follows:

if ([mPlayer respondsToSelector:@selector(setVolume:)]) {
    mPlayer.volume = 0.0;
 } else {
     NSArray *audioTracks = mPlayerItem.asset.tracks;

     // 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];

     [mPlayerItem setAudioMix:audioZeroMix]; // Mute the player item
 }
Related Topic