AVPlayer Volume Control

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

Why can't I use .volume with AVPlayer, only with AVAudioPlayer? Is there any other way to change the volume?

eg music.volume = 0.0;

Thank you for your responses.

+6
source share
5 answers

Starting with iOS 7, just call:

 myAvPlayer.volume = 0; 

Otherwise, you will have to use the annoying setAudioMix solution. I found support for this in my application 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 } 
+11
source

I used the code below to disable AVPlayer.

  float volSet = 0 ; AVAsset *avAsset = [[avPlayer currentItem] asset] ; NSArray *audioTracks = [avAsset tracksWithMediaType:AVMediaTypeAudio] ; NSMutableArray *allAudioParams = [NSMutableArray array] ; for(AVAssetTrack *track in audioTracks){ AVMutableAudioMixInputParameters *audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters] ; [audioInputParams setVolume:volSet atTime:kCMTimeZero] ; [audioInputParams setTrackID:[track trackID]] ; [allAudioParams addObject:audioInputParams]; } AVMutableAudioMix *audioVolMix = [AVMutableAudioMix audioMix] ; [audioVolMix setInputParameters:allAudioParams]; [[avPlayer currentItem] setAudioMix:audioVolMix]; 
+3
source

The best way:

  AVAssetTrack* assetTrackAudio = [arrayTracks objectAtIndex:0]; AVMutableAudioMixInputParameters* audioInputParams = [AVMutableAudioMixInputParameters audioMixInputParameters]; [audioInputParams setVolume:currentVolume atTime:playerCursorStartPosition]; [audioInputParams setTrackID:[assetTrackAudio trackID]]; NSArray* audioParams = [NSArray arrayWithObject:audioInputParams]; AVMutableAudioMix* audioMix = [AVMutableAudioMix audioMix]; [audioMix setInputParameters:audioParams]; AVPlayerItem* item = [player currentItem]; [item setAudioMix:audioMix]; 
+2
source

Check out this link

Apple mute documentation

Below is the code

  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]; 
0
source

Like iOS 7, AVPlayer has a muted property:

 @property (nonatomic, getter=isMuted) BOOL muted NS_AVAILABLE(10_7, 7_0); 
0
source

All Articles