How to mute video played in AVPlayer?

被刻印的时光 ゝ 提交于 2019-12-22 01:26:03

问题


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


回答1:


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




回答2:


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



回答3:


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;



回答4:


For Swift 4 above to make AVPlayer video mute

self.player.isMuted = true



回答5:


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



回答6:


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


来源:https://stackoverflow.com/questions/6120687/how-to-mute-video-played-in-avplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!