How to mute video played in AVPlayer?
Asked Answered
F

7

36

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

Fellmonger answered 25/5, 2011 at 7:0 Comment(1)
self.avPlayer.muted = true;Femoral
V
36

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

Video answered 28/1, 2015 at 3:34 Comment(2)
self.avPlayer.muted = true;Femoral
Updated for Swift 3Video
K
12

For Swift 4 above to make AVPlayer video mute

self.player.isMuted = true
Knotty answered 15/11, 2018 at 4:34 Comment(2)
This property does not mute the audio.Dodson
I my case i have tested above code and then posted here.Knotty
C
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];
Carom answered 4/6, 2011 at 4:0 Comment(4)
I have tried this method, but it is not functioning in the simulator. I'm using AVPlayer to play .mov file. Can you suggest any other solution.Fellmonger
I find that doing it this way interrupts any music you might be playing through Music app or iTunes, etc. Is there a way to continue music playing in the background?Martellato
Great, just change the first "AVURLAsset" to "AVAsset" and it's perfect.Preamplifier
This looks very complicated, no ?Mortimer
F
8

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;
Femoral answered 4/2, 2017 at 0:4 Comment(1)
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?Femoral
T
3

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
Thaxton answered 20/6, 2019 at 16:3 Comment(0)
H
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;
        }
    }
}
Harvard answered 4/8, 2018 at 17:11 Comment(0)
C
0

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

Cleaves answered 23/2 at 16:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.