Suresh
Suresh

Reputation: 369

How to mute video played in AVPlayer?

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

Upvotes: 36

Views: 21999

Answers (7)

yoAlex5
yoAlex5

Reputation: 34175

iOS mute/unmute audio

iOS devise has Silent mode. When Silent mode is On(red dot) by default device doesn't play audio.

When you open system's Photo app you find that:

  • Live Photo's sound is bounded with Silent mode. E.g when Silent mode is on, video's audio is off
  • Video's sound is not bounded with Silent mode. It is muted by default but you are able to change it(unmute) even when Silent mode is on

As for AVPlayer

You are able to change AVPlayer.isMuted property

self.player.isMuted = true

By default it is bounded with Silent mode. To play audio when Silent mode is on set audio playback category(e.g when app is started)

do {
    try AVAudioSession.sharedInstance().setCategory(AVAudioSession.Category.playback, mode: .default, options: [])
} catch {
    
}

Also check that AVLayer.volume is not zero to hear an audio track

Upvotes: 0

john raja
john raja

Reputation: 589

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

Upvotes: 4

Hardik Thakkar
Hardik Thakkar

Reputation: 15951

For Swift 4 above to make AVPlayer video mute

self.player.isMuted = true

Upvotes: 12

zszen
zszen

Reputation: 1468

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;
        }
    }
}

Upvotes: 0

bcattle
bcattle

Reputation: 12819

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

Upvotes: 36

BennyTheNerd
BennyTheNerd

Reputation: 4000

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;

Upvotes: 8

friedFingers
friedFingers

Reputation: 245

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

Upvotes: 10

Related Questions