代码之家  ›  专栏  ›  技术社区  ›  Devin Dixon

IOS Webkit无音频

  •  0
  • Devin Dixon  · 技术社区  · 3 年前

    我正在使用WebRTC获取音频和视频,但我遇到了音频静音的问题。它可以在Chrome和Android上运行,只在iOS中失败。我的代码如下:

        <wkWebView 
    contentMode="scaleToFill" 
    translatesAutoresizingMaskIntoConstraints="NO" id="iea-Pu-Gia">                    
    <rect key="frame" x="0.0" y="0.0" width="414" height="818"/>                    
    <color 
    key="backgroundColor" 
    red="0.36078431370000003" 
    green="0.38823529410000002" 
    blue="0.4039215686" 
    alpha="1" 
    'colorSpace="custom" 
    customColorSpace="sRGB"/>                    
    <wkWebViewConfiguration 
    key="configuration" 
    allowsInlineMediaPlayback="YES">                      
    <audiovisualMediaTypes 
    key="mediaTypesRequiringUserActionForPlayback" 
    audio="NO" video="NO"/>                      
    <wkPreferences key="preferences"/>                    
    </wkWebViewConfiguration>                  
    </wkWebView>
    

    有什么建议吗?我该配置什么来让音频在没有用户手势的情况下自动工作?

    0 回复  |  直到 3 年前
        1
  •  0
  •   Mike Irving    3 年前

    为了停止不必要的播放并延长电池寿命,默认情况下会强制执行此行为。

    您可以在代码中设置对此行为的覆盖,例如:

    let webConfiguration = WKWebViewConfiguration()
    webConfiguration.allowsInlineMediaPlayback = true
    if #available(iOS 10.0, *) {
        webConfiguration.mediaTypesRequiringUserActionForPlayback = []
    } else {
        // Fallback on earlier versions
        webConfiguration.mediaPlaybackRequiresUserAction = false
    }
    self.webView = WKWebView(frame: CGRectZero, configuration: webConfiguration)
    

    https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/1851524-mediatypesrequiringuseractionfor

    推荐文章