2017-09-04 71 views
1

我在那裏有一個WKWebView我顯示我的網站。我用js播放音效,但是當我這樣做時,背景音樂停止播放。我喜歡將這種音效與Spotify等背景音樂混合使用。爲WKWebview設置AVAudioSession類別

任何想法我怎麼做?

這是我在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. 
 
    } 
 

 

 
}

回答

0

代碼,我發現,這是不可能的。

相關問題