iosobjective-caudioavplayer

Turn off audio playback of AVPlayer?


I have a AVPlayer with AVPlayerItem. What I want is to turn off the audio playback in the AVPlayer. I want to play only video, no audio.

Here's what I have so far:

self.avPlayerItem = [AVPlayerItem playerItemWithURL:self.videoUrl];
self.avPlayer = [AVPlayer playerWithPlayerItem:self.avPlayerItem];
[self.avPlayer play];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;

self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
        
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playerItemDidPlayToEndTime:)                                           
                                             name:AVPlayerItemDidPlayToEndTimeNotification
                                           object:self.avPlayerItem];

CGRect screenRect = [[UIScreen mainScreen] bounds];

self.avPlayerLayer.frame = CGRectMake(0, 0, screenRect.size.width, screenRect.size.height);
[self.view.layer insertSublayer:self.avPlayerLayer atIndex:0];

Solution

  • AVPlayer have option

    @property (nonatomic, getter=isMuted) BOOL muted NS_AVAILABLE(10_7, 7_0);
    

    You can write

    - (void) muteSound:(BOOL)mute
    {
        self.avPlayer.muted = mute;
    }
    

    And use it, how you want

    - (void) startPlayingVideo
    {
    
        [self muteSound:YES];
    
        //other code
    
    }