如何静音AVPlayer中播放的视频?

时间:2011-05-25 07:00:34

标签: objective-c avplayer

我在AVPlayer中播放视频,现在我需要在播放时单独静音。请在目标C中建议如何做。

谢谢, 苏雷什

6 个答案:

答案 0 :(得分:24)

从iOS7开始,您可以将AVPlayer isMuted属性设置为true

在Objective C中,该属性称为muted

参考:https://developer.apple.com/documentation/avfoundation/avplayer/1387544-ismuted

答案 1 :(得分:9)

这应该会看到你...

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

答案 2 :(得分:4)

SWIFT 2.0和SWIFT 3.0 (截至2017年7月5日)

对于那些对Swift充满好奇的人来说,这只是:

self.avPlayer.muted = true

OBJECTIVE-C的最简单方法:

self.avPlayer.muted = true;

答案 3 :(得分:0)

您需要在视频处于播放状态时将false设置为false。

添加侦听器:

[itemPlayer addObserver:self
             forKeyPath:kStatusKey
                options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
                context:@"AVPlayerStatus"];

代码:

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

答案 4 :(得分:0)

要使上述Swift 4使AVPlayer视频静音

self.player.isMuted = true

答案 5 :(得分:0)

player.isMuted = true对我不起作用。

就我而言,我需要视频永久静音。所以我用下面的代码实现了这一点。

self.player.volume = 0.0