繁体   English   中英

为 WKWebview 设置 AVAudioSession 类别

[英]Set AVAudioSession Category for 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.
        }


    }

我发现这是不可能的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM