Reputation: 3718
How can I change the volume of the AVPlayer Dynamically? I mean, I want to mute the volume every time a button is pressed. the given code seems to change it in compile time only. How to do it during runtime???
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[self myAssetURL] options:nil];
NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];
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];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
[playerItem setAudioMix:audioZeroMix];
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
self.mPlayer = player;
[mPlayer play];
Upvotes: 3
Views: 2906
Reputation:
You can send playerItem
new instances of AVMutableAudioMix during playback to change levels dynamically. Just link your button to an action method that creates a new AVMutableAudioMix instance (like you have done above) with the appropriate values, and use playerItem's setAudioMix:
method to set the new mix values. (If you're working across methods, don't forget to save a reference to your playerItem instance to access it later.)
(N.B. setAudioMix:
isn't mentioned explicitly in the AVPlayerItem docs because it is a synthesized setter for the audioMix
property.)
Upvotes: 2