Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayer Volume Control

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.

like image 328
Lorenz Wöhr Avatar asked Mar 22 '13 12:03

Lorenz Wöhr


1 Answers

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
 }
like image 196
Doug Avatar answered Sep 18 '22 19:09

Doug