Im playing a video in AVPlayer, and now I need to mute the audio alone while playing it. Please suggest how to do in objective C.
Thanks, Suresh
Im playing a video in AVPlayer, and now I need to mute the audio alone while playing it. Please suggest how to do in objective C.
Thanks, Suresh
Since iOS7 you can set the AVPlayer isMuted
property to true
.
In Objective C the property is called muted
.
Reference: https://developer.apple.com/documentation/avfoundation/avplayer/1387544-ismuted
For Swift 4 above to make AVPlayer video mute
self.player.isMuted = true
This should see you through...
AVURLAsset *asset = [[avPlayer currentItem] asset];
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];
[[avPlayer currentItem] setAudioMix:audioZeroMix];
SWIFT 2.0 and SWIFT 3.0 (As of July 5, 2017)
For those of you curious about Swift it is simply just:
self.avPlayer.muted = true
EASIEST way for OBJECTIVE-C:
self.avPlayer.muted = true;
player.isMuted = true
is not working for me.
In my case I need the video permanently mute. So I used the below code to achieve this.
self.player.volume = 0.0
You need set muted false when the video is playing status.
add listener:
[itemPlayer addObserver:self
forKeyPath:kStatusKey
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:@"AVPlayerStatus"];
code:
-(void)observeValueForKeyPath:(NSString *)path ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == @"AVPlayerStatus") {
AVPlayerStatus status = [[change objectForKey:NSKeyValueChangeNewKey] integerValue];
switch (status) {
case AVPlayerStatusReadyToPlay: {
if (isMuted) {
layerPlayer.player.muted = true;
}
}
default:
break;
}
}
}