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

发布于 2024-11-09 17:30:42 字数 84 浏览 1 评论 0原文

我正在 AVPlayer 中播放视频,现在我需要在播放时单独静音音频。请建议如何在 Objective C 中进行操作。

谢谢, 苏雷什

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

羁绊已千年 2024-11-16 17:30:42

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

在 Objective C 中,该属性称为 muted

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

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

狼性发作 2024-11-16 17:30:42

对于Swift 4以上版本使AVPlayer视频静音

self.player.isMuted = true

For Swift 4 above to make AVPlayer video mute

self.player.isMuted = true
×眷恋的温暖 2024-11-16 17:30:42

这应该可以帮助你渡过难关...

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

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];
攒一口袋星星 2024-11-16 17:30:42

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

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

self.avPlayer.muted = true

OBJECTIVE-C 的最简单方法:

self.avPlayer.muted = true;

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;
夏夜暖风 2024-11-16 17:30:42

player.isMulated = true 不适合我。

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

self.player.volume = 0.0

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
栖竹 2024-11-16 17:30:42

当视频处于播放状态时,您需要设置 muted 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;
        }
    }
}

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;
        }
    }
}
染年凉城似染瑾 2024-11-16 17:30:42

iOS 音频静音/取消静音

iOS 设备具有静音模式。默认设备开启静音模式时(红点)不播放音频。

当您打开系统的照片应用程序时,您会发现:

  • Live Photo的声音与静音模式有关。例如,当静音模式打开时,视频的音频关闭
  • 视频的声音不受静音模式的限制。默认情况下它是静音的,但即使打开静音模式,您也可以更改它(取消静音)至于

AVPlayer

您可以更改 AVPlayer.isMulated 属性

self.player.isMuted = true

默认情况下,它与静默模式绑定。要在静音模式打开时播放音频,请设置音频播放类别(例如,当应用程序启动时)

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

还要检查 AVLayer.volume 是否不为零以听到音轨

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文