ios - 为 WKWebview 设置 AVAudioSession 类别

标签 ios swift wkwebview

我有一个 WKWebView 在那里我展示了我的网站,我用 js 播放了一个音效但是当我这样做时背景音乐停止播放。我喜欢将这种音效与 Spotify 等背景音乐混合在一起。

知道我该怎么做吗?

这是我在 ViewController.swift 中的代码

    import WebKit
    import AVFoundation

    class ViewController: UIViewController, WKNavigationDelegate {
        
        var webView: WKWebView!
        
        override func loadView() {
            webView = WKWebView()
            webView.navigationDelegate = self
            
            
            // Set so play inline works
            let webConfiguration = WKWebViewConfiguration()
            webConfiguration.allowsInlineMediaPlayback = true
            if #available(iOS 10.0, *) {
                webConfiguration.mediaTypesRequiringUserActionForPlayback = []
            } else {
                // Fallback on earlier versions
                webConfiguration.mediaPlaybackRequiresUserAction = false
            }
            webView = WKWebView(frame: CGRect.zero, configuration: webConfiguration)
            
            //super.init(coder: aDecoder)
            
            view = webView
            
        }
        

        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            
            // Set so we can play sound effect without stop background music
            
            
            // Get the local lang from iphone
            var locale = Locale.preferredLanguages[0]
            if (locale.contains("-")) {
                locale = locale.substring(to: locale.characters.index(locale.startIndex, offsetBy: 2))
            }
            
            
            let url = URL(string: "https://test.com/?lang=" + locale)!
            webView.load(URLRequest(url: url))
            webView.allowsBackForwardNavigationGestures = true
            
            // add background color to wkwebview to transparent
            webView.backgroundColor = UIColor.clear
            webView.scrollView.backgroundColor = UIColor.clear
            
            
            // Change the statusbar to vit
            UIApplication.shared.statusBarStyle = .lightContent
            
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }


    }

最佳答案

我发现这是不可能的。

关于ios - 为 WKWebview 设置 AVAudioSession 类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46033204/

相关文章:

ios - iOS 8 上的 UIActivityViewController 显示带有自定义事件的 "more"按钮

c++ - Objective-C和C++混合编译错误 “_OBJC_CLASS_$_UIResponder”

ios - 如何动态添加swift字典中的值

ios - 从 WKWebview 检索 HTTP 响应 header

javascript - 将 JavaScript 与 WkWebView 一起使用 - UserMessageHandler 上出现错误

ios - 更改 UITabBarController 中选项卡的顺序

ios - SwiftUI 转义闭包捕获变异的 'self' 参数

swift - 如何在 Swift 中按名字按字母顺序排列数组?

ios - View 上的模糊背景无法始终如一地工作

ios - 禁用 WKWebView 缩放