32

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

Suresh
  • 321
  • 1
  • 3
  • 3

6 Answers6

34

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

bcattle
  • 12,115
  • 6
  • 62
  • 82
11

For Swift 4 above to make AVPlayer video mute

self.player.isMuted = true
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
10

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];
friedFingers
  • 245
  • 6
  • 18
7

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;
BennyTheNerd
  • 3,930
  • 1
  • 21
  • 16
  • Sorry it's not working for you Vineesh. It's working on my Swift 3.0 and 3.1. Could it be other variables you have going there? – BennyTheNerd Sep 11 '17 at 16:18
2

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
john raja
  • 509
  • 4
  • 8
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;
        }
    }
}
zszen
  • 1,428
  • 1
  • 14
  • 18