【问题标题】:Can I mute for youtube-ios-player-helper?我可以为 youtube-ios-player-helper 静音吗?
【发布时间】:2025-11-26 22:50:02
【问题描述】:

我正在使用 youtube-ios-player-helper 在我的应用程序中播放 youtube 视频。我希望能够静音。这是我所做的:

  1. 在 YTPlayerView 中添加 setVolume() 和 mute() 函数

    -(void)setVolume:(int)volume {
    
        if ( volume < 0 )
            volume = 0;
        else if ( volume > 100 )
            volume = 100;
        NSString *command = [NSString stringWithFormat:@"player.setVolume(%d);", volume];
        [self stringFromEvaluatingJavaScript:command];
    }
    
    -(void)mute {        
        NSString *command = [NSString stringWithFormat:@"player.mute();"];
        [self stringFromEvaluatingJavaScript:command];
    }
    

然后我在我的应用中调用 setVolume:0mute 函数。但是声音无法关闭。音量还是100。

有人成功关掉声音了吗?

【问题讨论】:

  • 在项目的 GitHub 页面上查看此问题:github.com/youtube/youtube-ios-player-helper/issues/20
  • 你找到合适的解决方案了吗?
  • 我也是,你找到解决方法了吗?
  • 我使用了与您使用的完全相同的静音功能,但只有在我开始播放视频后立即调用该方法时它才对我有效,并且在我调用播放时必须准备好。编辑:我在webView 属性上调用了stringFromEvaluatingJavaScript 函数。

标签: ios youtube-api


【解决方案1】:

您必须在播放器字典中添加静音值

NSDictionary *playerVars = @{
      @"controls" : @0,
      @"playsinline" : @1,
      @"autohide" : @1,
      @"showinfo" : @0,
      @"modestbranding" : @1,
      @"mute": @1
  };

它在 iOS 中对我有用

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

您好,您可以将 youtube 视频静音 我已将该功能添加到 YTPlayerView https://github.com/abhay-singh/youtube-ios-player-helper/blob/master/youtube-ios-player-helper/YTPlayerView%2BMute_unMute.h

您可以从此链接https://github.com/abhay-singh/youtube-ios-player-helper.git克隆或下载该文件

【讨论】:

    【解决方案3】:

    按照上面的方法,我得到了它在 Swift4 中的工作:

    func playerView(_ playerView: YTPlayerView, didChangeTo state: YTPlayerState) {
        if case .playing = state {
            playerView.webView?.stringByEvaluatingJavaScript(from: "player.mute();")
        }
    }
    

    【讨论】: